Skip to content

Remove references to static Logger.Create. #386

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Text.Json;
using AutoFixture;
using Microsoft.Performance.SDK.Processing;
using Microsoft.Performance.SDK.Runtime;
using Microsoft.Performance.Testing;
using Microsoft.Performance.Toolkit.Plugins.Core;
using Microsoft.Performance.Toolkit.Plugins.Core.Metadata;
using Microsoft.Performance.Toolkit.Plugins.Core.Serialization;
using Microsoft.Performance.Toolkit.Plugins.Runtime.Exceptions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System.Text.Json;
using AutoFixture;
using Fixture = AutoFixture.Fixture;
using Microsoft.Performance.Toolkit.Plugins.Core;
using Microsoft.Performance.Toolkit.Plugins.Core.Metadata;

namespace Microsoft.Performance.Toolkit.Plugins.Runtime.Tests
{
Expand Down Expand Up @@ -64,7 +65,7 @@ public async Task GetAllAsync_MissingData_ThrowsRepositoryCorruptedException()
fakeSerializer.Setup(x => x.DeserializeAsync(It.IsAny<Stream>(), It.IsAny<CancellationToken>()))
.Throws<ArgumentNullException>();

var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object);
var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object, Logger.Create);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can extract a method for creating FileBackedPluginRegistry that will make modifying the constructor much easier in the future. Also we can avoid running the actual logging logic using Mock<ILooger>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, I prefer having a CreateSut() method


await Assert.ThrowsExceptionAsync<RepositoryCorruptedException>(() => sut.GetAllAsync(CancellationToken.None));
}
Expand All @@ -76,7 +77,8 @@ public async Task GetAllAsync_FileNotExist_ReturnsEmpty()
var fakeSerializer = new Mock<ISerializer<List<InstalledPluginInfo>>>();
var sut = new FileBackedPluginRegistry(
this.registryRoot,
fakeSerializer.Object);
fakeSerializer.Object,
Logger.Create);

IReadOnlyCollection<InstalledPluginInfo> result = await sut.GetAllAsync(CancellationToken.None);

Expand All @@ -93,7 +95,7 @@ public async Task GetAllAsync_FileOpenedByOtherProcess_ThrowsDataAccessException
fakeSerializer.Setup(x => x.DeserializeAsync(It.IsAny<Stream>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<InstalledPluginInfo>());

var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object);
var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object, Logger.Create);

await Assert.ThrowsExceptionAsync<RepositoryDataAccessException>(() => sut.GetAllAsync(CancellationToken.None));
}
Expand Down Expand Up @@ -138,7 +140,7 @@ public async Task GetAllAsync_ReturnsRegisteredPluginsInfo()
fakeSerializer.Setup(x => x.DeserializeAsync(It.IsAny<Stream>(), It.IsAny<CancellationToken>()).Result)
.Returns(new List<InstalledPluginInfo>(expectedResult));

var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object);
var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object, Logger.Create);

IReadOnlyCollection<InstalledPluginInfo> actualResult = await sut.GetAllAsync(CancellationToken.None);

Expand All @@ -161,7 +163,7 @@ public async Task TryGetByIdAsync_PluginExists_Returns()
fakeSerializer.Setup(x => x.DeserializeAsync(It.IsAny<Stream>(), It.IsAny<CancellationToken>()).Result)
.Returns(new List<InstalledPluginInfo>() { fakeInstalledPluginInfo });

var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object);
var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object, Logger.Create);

InstalledPluginInfo actualResult = await sut.TryGetByIdAsync(fakeInstalledPluginInfo.Metadata.Identity.Id, CancellationToken.None);

Expand All @@ -179,7 +181,7 @@ public async Task TryGetByIdAsync_PluginDoesNotExist_ReturnsNull()
fakeSerializer.Setup(x => x.DeserializeAsync(It.IsAny<Stream>(), It.IsAny<CancellationToken>()).Result)
.Returns(new List<InstalledPluginInfo>());

var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object);
var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object, Logger.Create);

InstalledPluginInfo actualResult = await sut.TryGetByIdAsync(fakeInstalledPluginInfo.Metadata.Identity.Id, CancellationToken.None);

Expand All @@ -197,7 +199,7 @@ public async Task TryGetByIdAsync_DuplicatesDetected_ThrowsRepositoryCorruptedEx
fakeSerializer.Setup(x => x.DeserializeAsync(It.IsAny<Stream>(), It.IsAny<CancellationToken>()).Result)
.Returns(new List<InstalledPluginInfo>() { fakeInstalledPluginInfo, fakeInstalledPluginInfo });

var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object);
var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object, Logger.Create);

await Assert.ThrowsExceptionAsync<RepositoryCorruptedException>(() => sut.TryGetByIdAsync(fakeInstalledPluginInfo.Metadata.Identity.Id, CancellationToken.None));
}
Expand All @@ -217,7 +219,7 @@ public async Task ExistsAsync_Exists_ReturnsTrue()
fakeSerializer.Setup(x => x.DeserializeAsync(It.IsAny<Stream>(), It.IsAny<CancellationToken>()).Result)
.Returns(new List<InstalledPluginInfo>() { fakeInstalledPluginInfo });

var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object);
var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object, Logger.Create);

bool actualResult = await sut.ExistsAsync(fakeInstalledPluginInfo, CancellationToken.None);

Expand All @@ -235,7 +237,7 @@ public async Task ExistsAsync_DoesNotExist_ReturnsFalse()
fakeSerializer.Setup(x => x.DeserializeAsync(It.IsAny<Stream>(), It.IsAny<CancellationToken>()).Result)
.Returns(new List<InstalledPluginInfo>());

var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object);
var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object, Logger.Create);

bool actualResult = await sut.ExistsAsync(fakeInstalledPluginInfo, CancellationToken.None);

Expand All @@ -253,7 +255,7 @@ public async Task ExistsAsync_DuplicatesDetected_ThrowsRepositoryCorruptedExcept
fakeSerializer.Setup(x => x.DeserializeAsync(It.IsAny<Stream>(), It.IsAny<CancellationToken>()).Result)
.Returns(new List<InstalledPluginInfo>() { fakeInstalledPluginInfo, fakeInstalledPluginInfo });

var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object);
var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object, Logger.Create);

await Assert.ThrowsExceptionAsync<RepositoryCorruptedException>(() => sut.ExistsAsync(fakeInstalledPluginInfo, CancellationToken.None));
}
Expand All @@ -269,7 +271,7 @@ public async Task AddAsync_FileNotExist_CreatesFilePluginAdded()
var fakeSerializer = new Mock<ISerializer<List<InstalledPluginInfo>>>();
InstalledPluginInfo fakeInstalledPluginInfo = new Fixture().Create<InstalledPluginInfo>();

var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object);
var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object, Logger.Create);

await sut.AddAsync(fakeInstalledPluginInfo, CancellationToken.None);

Expand Down Expand Up @@ -297,7 +299,7 @@ public async Task AddAsync_PluginAdded()
plugin1,
});

var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object);
var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object, Logger.Create);

await sut.AddAsync(plugin2, CancellationToken.None);

Expand Down Expand Up @@ -325,7 +327,7 @@ public async Task AddAsync_AlreadyRegisteredPlugin_ThrowsInvalidOperationExcepti
plugin2
});

var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object);
var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object, Logger.Create);

await Assert.ThrowsExceptionAsync<InvalidOperationException>(() => sut.AddAsync(plugin1, CancellationToken.None));
}
Expand All @@ -341,7 +343,7 @@ public async Task DeleteAsync_FileNotExist_ThrowsInvalidOperationException()
var fakeSerializer = new Mock<ISerializer<List<InstalledPluginInfo>>>();
InstalledPluginInfo fakeInstalledPluginInfo = new Fixture().Create<InstalledPluginInfo>();

var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object);
var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object, Logger.Create);

await Assert.ThrowsExceptionAsync<InvalidOperationException>(
() => sut.DeleteAsync(fakeInstalledPluginInfo, CancellationToken.None));
Expand All @@ -365,7 +367,7 @@ public async Task DeleteAsync_PluginDeleted()
plugin2,
});

var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object);
var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object, Logger.Create);

await sut.DeleteAsync(plugin1, CancellationToken.None);

Expand All @@ -392,7 +394,7 @@ public async Task DeleteAsync_PluginNotRegistered_ThrowsInvalidOperationExceptio
plugin1,
});

var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object);
var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object, Logger.Create);

await Assert.ThrowsExceptionAsync<InvalidOperationException>(() => sut.DeleteAsync(plugin2, CancellationToken.None));
}
Expand All @@ -415,7 +417,7 @@ public async Task DeleteAsync_PluginWithSameIdButDifferentVersionRegistered_Thro
plugin1,
});

var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object);
var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object, Logger.Create);

await Assert.ThrowsExceptionAsync<InvalidOperationException>(() => sut.DeleteAsync(plugin2, CancellationToken.None));
}
Expand All @@ -433,7 +435,7 @@ public async Task UpdateAsync_DifferentIDs_ThrowsInvalidOperationException()
InstalledPluginInfo fakeInstalledPluginInfo = new Fixture().Create<InstalledPluginInfo>();
var fakeUpdatedPluginInfo = CreatePluginWithDifferentIdVersion(fakeInstalledPluginInfo);

var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object);
var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object, Logger.Create);

await Assert.ThrowsExceptionAsync<InvalidOperationException>(
() => sut.UpdateAsync(fakeInstalledPluginInfo, fakeUpdatedPluginInfo, CancellationToken.None));
Expand All @@ -448,7 +450,7 @@ public async Task UpdateAsync_FileNotExist_ThrowsInvalidOperationException()
InstalledPluginInfo fakeInstalledPluginInfo = new Fixture().Create<InstalledPluginInfo>();
var toUpdate = CreatePluginWithDifferentInstallDate(fakeInstalledPluginInfo);

var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object);
var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object, Logger.Create);

await Assert.ThrowsExceptionAsync<InvalidOperationException>(
() => sut.UpdateAsync(fakeInstalledPluginInfo, toUpdate, CancellationToken.None));
Expand All @@ -467,7 +469,7 @@ public async Task UpdateAsync_CurrentPluginNotRegistered_ThrowsInvalidOperationE
fakeSerializer.Setup(x => x.DeserializeAsync(It.IsAny<Stream>(), It.IsAny<CancellationToken>()).Result)
.Returns(new List<InstalledPluginInfo>());

var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object);
var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object, Logger.Create);

await Assert.ThrowsExceptionAsync<InvalidOperationException>(() => sut.UpdateAsync(current, toUpdate, CancellationToken.None));
}
Expand All @@ -488,7 +490,7 @@ public async Task UpdateAsync_ToUpdatePluginAlreadyRegistered_ThrowsInvalidOpera
toUpdate,
});

var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object);
var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object, Logger.Create);

await Assert.ThrowsExceptionAsync<InvalidOperationException>(() => sut.UpdateAsync(current, toUpdate, CancellationToken.None));
}
Expand All @@ -509,7 +511,7 @@ public async Task UpdateAsync_PluginUpdated()
plugin1,
});

var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object);
var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object, Logger.Create);

await sut.UpdateAsync(plugin1, plugin2, CancellationToken.None);

Expand All @@ -528,7 +530,7 @@ public async Task UpdateAsync_PluginUpdated()
public async Task AquireLock_Succeeds()
{
var fakeSerializer = new Mock<ISerializer<List<InstalledPluginInfo>>>();
var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object);
var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object, Logger.Create);

using (IDisposable _ = await sut.AquireLockAsync(CancellationToken.None, null))
{
Expand All @@ -543,7 +545,7 @@ public async Task AquireLock_Succeeds()
public async Task AquireLock_Fails()
{
var fakeSerializer = new Mock<ISerializer<List<InstalledPluginInfo>>>();
var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object);
var sut = new FileBackedPluginRegistry(this.registryRoot, fakeSerializer.Object, Logger.Create);

Task<IDisposable> attempt;
using (IDisposable _ = await sut.AquireLockAsync(CancellationToken.None, null))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System.Collections.Specialized;
using Microsoft.Performance.SDK.Runtime;
using Microsoft.Performance.Testing;
using Microsoft.Performance.Toolkit.Plugins.Core.Discovery;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand All @@ -17,7 +18,7 @@ public class PluginSourcesRepositoryTests
[UnitTest]
public void Add_Single_New_Added()
{
var repo = new PluginSourceRepository();
var repo = new PluginSourceRepository(Logger.Create);
var source = new PluginSource(FakeUris.Uri1);

bool success = repo.Add(source);
Expand Down Expand Up @@ -55,7 +56,7 @@ public void Add_Single_Duplicate_NotAdded()
[UnitTest]
public void Add_Single_CollectionChangedInvoked()
{
var repo = new PluginSourceRepository();
var repo = new PluginSourceRepository(Logger.Create);
var source = new PluginSource(FakeUris.Uri1);
bool raised = false;

Expand Down Expand Up @@ -91,7 +92,7 @@ public void Add_Single_Fails_CollectionChangedNotInvoked()
[UnitTest]
public void Add_Single_NullItem_ThrowsArgumentNullException()
{
var repo = new PluginSourceRepository();
var repo = new PluginSourceRepository(Logger.Create);

PluginSource? pluginSource = null;
Assert.ThrowsException<ArgumentNullException>(() => repo.Add(pluginSource));
Expand All @@ -104,7 +105,7 @@ public void Add_Single_NullItem_ThrowsArgumentNullException()
[UnitTest]
public void Add_Multiple_New_Added()
{
var repo = new PluginSourceRepository();
var repo = new PluginSourceRepository(Logger.Create);
var sources = new PluginSource[]
{
new PluginSource(FakeUris.Uri1),
Expand Down Expand Up @@ -137,7 +138,7 @@ public void Add_Multiple_Existing_NewItemsAdded()
[UnitTest]
public void Add_Multiple_CollectionChangedInvoked()
{
var repo = new PluginSourceRepository();
var repo = new PluginSourceRepository(Logger.Create);
var sources = new PluginSource[]
{
new PluginSource(FakeUris.Uri1),
Expand Down Expand Up @@ -176,7 +177,7 @@ public void Add_Multiple_NoneAdded_CollectionChangedNotInvoked()
[UnitTest]
public void Add_Mutiple_ThrowsArgumentNullException()
{
var repo = new PluginSourceRepository();
var repo = new PluginSourceRepository(Logger.Create);

PluginSource[]? pluginSources = null;
Assert.ThrowsException<ArgumentNullException>(() => repo.Add(pluginSources));
Expand Down Expand Up @@ -267,7 +268,7 @@ public void Remove_Single_NoneRemoved_CollectionChangedNotInvoked()
[UnitTest]
public void Remove_Single_ThrowsArgumentNullException()
{
var repo = new PluginSourceRepository();
var repo = new PluginSourceRepository(Logger.Create);

PluginSource? pluginSource = null;
Assert.ThrowsException<ArgumentNullException>(() => repo.Remove(pluginSource));
Expand Down Expand Up @@ -361,7 +362,7 @@ public void Remove_Multiple_NoneRemoved_CollectionChangedNotInvoked()
[UnitTest]
public void Remove_Multiple_ThrowsArgumentNullException()
{
var repo = new PluginSourceRepository();
var repo = new PluginSourceRepository(Logger.Create);

PluginSource[]? pluginSources = null;
Assert.ThrowsException<ArgumentNullException>(() => repo.Remove(pluginSources));
Expand All @@ -376,7 +377,7 @@ public void Remove_Multiple_ThrowsArgumentNullException()
[UnitTest]
public void CollectionChanged_NewItem_Correct()
{
var repo = new PluginSourceRepository();
var repo = new PluginSourceRepository(Logger.Create);
var source = new PluginSource(FakeUris.Uri1);

repo.CollectionChanged += (s, e) =>
Expand All @@ -392,7 +393,7 @@ public void CollectionChanged_NewItem_Correct()
[UnitTest]
public void CollectionChanged_NewItems_Correct()
{
var repo = new PluginSourceRepository();
var repo = new PluginSourceRepository(Logger.Create);
var sources = new[]
{
new PluginSource(FakeUris.Uri1),
Expand Down Expand Up @@ -450,7 +451,7 @@ public void CollectionChanged_OldItems_Correct()

private PluginSourceRepository CreateSut(IEnumerable<PluginSource>? existingPluginSources)
{
var repo = new PluginSourceRepository();
var repo = new PluginSourceRepository(Logger.Create);
if (existingPluginSources != null && existingPluginSources.Any())
{
repo.Add(existingPluginSources);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ private async Task<IEnumerable<IPluginDiscoverer>> CreateDiscoverers(

Debug.Assert(discoverer != null);

discoverer.SetLogger(Logger.Create(discoverer.GetType()));
discoverer.SetLogger(this.loggerFactory.Invoke(discoverer.GetType()));
results.Add(discoverer);
}

Expand Down
Loading
Loading