Skip to content

Commit

Permalink
w
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenWeatherford committed Jan 24, 2025
1 parent c70a7ec commit bbaf353
Show file tree
Hide file tree
Showing 17 changed files with 272 additions and 279 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,16 @@ private static CompilationResult Compile(
var publicModuleMetadataProvider = StrictMock.Of<IPublicModuleMetadataProvider>();
publicModuleMetadataProvider.Setup(x => x.GetCachedModules())
.Returns([.. availableModules
.Select(m => new DefaultRegistryModuleMetadata(
.Select(m => new RegistryModuleMetadata(
"mcr.microsoft.com",
m,
new RegistryMetadataDetails(null, null),
[.. availableVersions
.Select(v => new RegistryModuleVersionMetadata(v, new("det", "doc.html")))]))]);
//asdfg
//publicModuleMetadataProvider.Setup(x => x.GetCachedModuleVersions(It.IsAny<string>()))
// .Returns((string module) =>
// {
// return availableModules.Contains(module) ?
// [.. availableVersions.Select(v => new RegistryModuleVersionMetadata(v, null, null))] :
// [];
// });
new RegistryModuleMetadata.ComputedData(
new RegistryMetadataDetails(null, null),
[.. availableVersions
.Select(v => new RegistryModuleVersionMetadata(v, new("det", "doc.html")))]
)))
]);

publicModuleMetadataProvider.Setup(x => x.IsCached)
.Returns(availableModules.Length > 0);
publicModuleMetadataProvider.Setup(x => x.DownloadError)
Expand Down
34 changes: 21 additions & 13 deletions src/Bicep.Core.UnitTests/Mock/Registry/RegistryCatalogMocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,20 @@ public static Mock<IPublicModuleMetadataProvider> MockPublicMetadataProvider(
publicProvider.Setup(x => x.Registry).Returns(PublicRegistry);

publicProvider.Setup(x => x.TryGetModulesAsync())
.ReturnsAsync([.. modules
.Select(m => new DefaultRegistryModuleMetadata(
.ReturnsAsync(
[.. modules
.Select(m => new RegistryModuleMetadata(
PublicRegistry,
m.moduleName,
getDetailsAsyncFunc: () => Task.FromResult(new RegistryMetadataDetails(m.description, m.documentationUri)),
getVersionsAsyncFunc: () => Task.FromResult<ImmutableArray<RegistryModuleVersionMetadata>>(
new RegistryModuleMetadata.ComputedData(
new RegistryMetadataDetails(m.description, m.documentationUri),
[.. modules.Single(m2 => m2.moduleName.EqualsOrdinally(m.moduleName))
.versions
.Select(v => new RegistryModuleVersionMetadata(v.version,new(v.description,v.documentUri)))])
))]);
.Select(v => new RegistryModuleVersionMetadata(v.version,new(v.description,v.documentUri)))
]
)
))]
);

return publicProvider;
}
Expand All @@ -63,15 +67,19 @@ public static Mock<IRegistryModuleMetadataProvider> MockPrivateMetadataProvider(

privateProvider.Setup(x => x.TryGetModulesAsync())
.ReturnsAsync([..
modules.Select(m => new DefaultRegistryModuleMetadata(
modules.Select(m => new RegistryModuleMetadata(
registry,
m.moduleName,
getDetailsAsyncFunc: async () => await DelayedValue(new RegistryMetadataDetails( m.description, m.documentationUri)),
getVersionsAsyncFunc: async () => await DelayedValue<ImmutableArray<RegistryModuleVersionMetadata>>(
[.. modules.Single(m => m.moduleName.EqualsOrdinally(m.moduleName))
.versions
.Select(v => new RegistryModuleVersionMetadata(v.version, new( v.description, v.documentUri)))])
))]);
getDataAsyncFunc: async () =>
new RegistryModuleMetadata.ComputedData(
await DelayedValue(new RegistryMetadataDetails( m.description, m.documentationUri)),
[.. modules.Single(m => m.moduleName.EqualsOrdinally(m.moduleName))
.versions
.Select(v => new RegistryModuleVersionMetadata(v.version, new(v.description, v.documentUri))
)]
)
))
]);

return privateProvider;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public async Task TryGetModulesAsync_ShouldCacheResult()
}

[TestMethod]
public async Task GetDetails()
public async Task GetDetails_ShouldBeCached()
{
var containerClient = new FakeContainerRegistryClient();
var clientFactory = await RegistryHelper.CreateMockRegistryClientWithPublishedModulesAsync(
Expand All @@ -318,13 +318,92 @@ public async Task GetDetails()
BicepTestConstants.BuiltInConfiguration.Cloud,
"registry.contoso.io",
clientFactory);
provider.GetCachedModules().Should().BeEmpty();

var module = await provider.TryGetModuleAsync("test/module1");
module.Should().NotBeNull();
provider.GetCachedModules().Should().NotBeEmpty();

var details = await module!.TryGetDetailsAsync();
details.Description.Should().Be("this is module 1 version 2");
details.DocumentationUri.Should().Be("http://contoso.com/help12");
containerClient.CallsToGetAllManifestPropertiesAsync.Should().Be(1);

// Verify it's cached
var details2 = await module!.TryGetDetailsAsync();
details.Should().BeSameAs(details2);
containerClient.CallsToGetAllManifestPropertiesAsync.Should().Be(1);

var cached = provider.GetCachedModules();
provider.GetCachedModules().Should().NotBeEmpty();
containerClient.CallsToGetAllManifestPropertiesAsync.Should().Be(1);
}

[TestMethod]
public async Task GetVersions_ShouldBeCached()
{
var containerClient = new FakeContainerRegistryClient();
var clientFactory = await RegistryHelper.CreateMockRegistryClientWithPublishedModulesAsync(
new MockFileSystem(),
containerClient,
[
new("br:registry.contoso.io/test/module1:v1", "metadata description = 'this is module 1 version 1'\nparam p1 bool", WithSource: true, DocumentationUri: "http://contoso.com/help11"),
new("br:registry.contoso.io/test/module2:v1", "metadata description = 'this is module 2 version 1'\nparam p2 string", WithSource: true, DocumentationUri: "http://contoso.com/help21"),
new("br:registry.contoso.io/test/module1:v2", "metadata description = 'this is module 1 version 2'\nparam p12 string", WithSource: false, DocumentationUri: "http://contoso.com/help12"),
]);

var provider = new PrivateAcrModuleMetadataProvider(
BicepTestConstants.BuiltInConfiguration.Cloud,
"registry.contoso.io",
clientFactory);

var module = await provider.TryGetModuleAsync("test/module1");
module.Should().NotBeNull();
module!.GetCachedVersions().Should().BeEmpty();
containerClient.CallsToGetAllManifestPropertiesAsync.Should().Be(0);
module.GetCachedVersions().Should().BeEmpty();

var versions = await module!.TryGetVersionsAsync();
containerClient.CallsToGetAllManifestPropertiesAsync.Should().Be(1);
versions[0].Version.Should().Be("v1");
module.GetCachedVersions()[0].Version.Should().Be("v1");

var versions2 = await module!.TryGetVersionsAsync();
containerClient.CallsToGetAllManifestPropertiesAsync.Should().Be(1);
versions[0].Version.Should().Be("v1");
module.GetCachedVersions()[0].Version.Should().Be("v1");
versions.Should().BeEquivalentTo(versions2);
versions[0].Version.Should().BeSameAs(versions2[0].Version);
}

[TestMethod]
public async Task GetDetails_ShouldAlsoCacheVersions_BecauseGettingModuleDetailsRequiresGettingVersionDetails()
{
var containerClient = new FakeContainerRegistryClient();
var clientFactory = await RegistryHelper.CreateMockRegistryClientWithPublishedModulesAsync(
new MockFileSystem(),
containerClient,
[
new("br:registry.contoso.io/test/module1:v1", "metadata description = 'this is module 1 version 1'\nparam p1 bool", WithSource: true, DocumentationUri: "http://contoso.com/help11"),
new("br:registry.contoso.io/test/module2:v1", "metadata description = 'this is module 2 version 1'\nparam p2 string", WithSource: true, DocumentationUri: "http://contoso.com/help21"),
new("br:registry.contoso.io/test/module1:v2", "metadata description = 'this is module 1 version 2'\nparam p12 string", WithSource: false, DocumentationUri: "http://contoso.com/help12"),
]);

var provider = new PrivateAcrModuleMetadataProvider(
BicepTestConstants.BuiltInConfiguration.Cloud,
"registry.contoso.io",
clientFactory);

var module = await provider.TryGetModuleAsync("test/module1");
module.Should().NotBeNull();
module!.GetCachedVersions().Should().BeEmpty();

var details = await module!.TryGetDetailsAsync();
details.Description.Should().Be("this is module 1 version 2");
containerClient.CallsToGetAllManifestPropertiesAsync.Should().Be(1);

var versions = await module!.TryGetVersionsAsync();
containerClient.CallsToGetAllManifestPropertiesAsync.Should().Be(1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1096,64 +1096,6 @@ public static void ClassInitialize(TestContext _)
.Respond("application/json", ModuleIndexJson);
}

[TestMethod]
public void GetExponentialDelay_ZeroCount_ShouldGiveInitialDelay()
{
TimeSpan initial = TimeSpan.FromDays(2.5);
TimeSpan max = TimeSpan.FromDays(10);
var delay = BaseModuleMetadataProvider.GetExponentialDelay(initial, 0, max);

delay.Should().Be(initial);
}

[TestMethod]
public void GetExponentialDelay_1Count_ShouldGiveDoubleInitialDelay()
{
TimeSpan initial = TimeSpan.FromDays(2.5);
TimeSpan max = TimeSpan.FromDays(10);
var delay = BaseModuleMetadataProvider.GetExponentialDelay(initial, 1, max);

delay.Should().Be(initial * 2);
}

[TestMethod]
public void GetExponentialDelay_2Count_ShouldGiveQuadrupleInitialDelay()
{
TimeSpan initial = TimeSpan.FromDays(2.5);
TimeSpan max = TimeSpan.FromDays(10);
var delay = BaseModuleMetadataProvider.GetExponentialDelay(initial, 2, max);

delay.Should().Be(initial * 4);
}

[TestMethod]
public void GetExponentialDelay_AboveMaxCount_ShouldGiveMaxDelay()
{
TimeSpan initial = TimeSpan.FromSeconds(1);
TimeSpan max = TimeSpan.FromDays(365);

TimeSpan exponentiallyGrowingDelay = initial;
int count = 0;
while (exponentiallyGrowingDelay < max * 1000)
{
var delay = PublicModuleMetadataProvider.GetExponentialDelay(initial, count, max);

if (exponentiallyGrowingDelay < max)
{
delay.Should().Be(exponentiallyGrowingDelay);
}
else
{
delay.Should().Be(max);
}

delay.Should().BeLessThanOrEqualTo(max);

++count;
exponentiallyGrowingDelay *= 2;
}
}

private record ModuleMetadata_Original(string moduleName, List<string> tags);

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
namespace Bicep.Core.UnitTests.Registry.Catalog
{
[TestClass]
public class RegistryCatalogTests //asdfg2
public class RegistryCatalogTests
{
[TestMethod]
public void GetRegistry_ShouldReturnSameObjectEachTime()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public FakeContainerRegistryClient() : base()
}

public int CallsToGetRepositoryNamesAsync { get; private set; }
public int CallsToGetAllManifestPropertiesAsync { get; private set; }

public SortedList<string, FakeRepository> FakeRepositories { get; } = new();

Expand All @@ -45,6 +46,8 @@ public override ContainerRepository GetRepository(string repositoryName)
repository.Setup(x => x.GetAllManifestPropertiesAsync(It.IsAny<ArtifactManifestOrder>(), It.IsAny<CancellationToken>()))
.Returns((ArtifactManifestOrder order, CancellationToken token) =>
{
CallsToGetAllManifestPropertiesAsync++;

var constructor = typeof(ArtifactManifestProperties).GetConstructor(
BindingFlags.NonPublic | BindingFlags.Instance,
null,
Expand Down
4 changes: 2 additions & 2 deletions src/Bicep.Core/Registry/Catalog/BaseModuleMetadataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public async Task<bool> TryUpdateCacheAsync()//asdfg implement/test threading
}
else
{
return false;//asdfg testpoint
return false;
}
}

Expand All @@ -79,7 +79,7 @@ public async Task<bool> TryUpdateCacheAsync()//asdfg implement/test threading
&& x.ModuleName.Equals(modulePath, StringComparison.Ordinal));
if (found != null && throwIfNotFound)
{
throw new InvalidOperationException($"Module '{modulePath}' not found in cache.");//asdfg testpoint
throw new InvalidOperationException($"Module '{modulePath}' not found in cache.");
}

return found;
Expand Down
103 changes: 0 additions & 103 deletions src/Bicep.Core/Registry/Catalog/DefaultRegistryModuleMetadata.cs

This file was deleted.

Loading

0 comments on commit bbaf353

Please sign in to comment.