Skip to content
Merged
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
@@ -0,0 +1,33 @@
namespace Particular.LicensingComponent.UnitTests;

using System.Collections.Generic;
using System.Threading.Tasks;
using NUnit.Framework;
using Particular.LicensingComponent.Contracts;
using Particular.LicensingComponent.UnitTests.Infrastructure;

/// <summary>
/// The report adds its environment data to the broker data. The data store can return a shared
/// instance, so the report must not change the broker data that the store holds. Otherwise two
/// reports that are generated at the same time write into the same dictionary.
/// </summary>
[TestFixture]
class ThroughputCollector_Report_BrokerMetadata_Tests : ThroughputCollectorTestFixture
{
[Test]
public async Task Should_not_change_the_stored_broker_data()
{
await DataStore.SaveBrokerMetadata(new BrokerMetadata("scope", new Dictionary<string, string> { ["Broker.Key"] = "value" }));

var report = await ThroughputCollector.GenerateThroughputReport("1.2.3", null);

var stored = await DataStore.GetBrokerMetadata();

using (Assert.EnterMultipleScope())
{
Assert.That(report.ReportData.EnvironmentInformation.EnvironmentData, Does.ContainKey("Broker.Key"), "The report includes the broker data");
Assert.That(report.ReportData.EnvironmentInformation.EnvironmentData, Does.ContainKey(EnvironmentDataType.ServicePulseVersion.ToString()));
Assert.That(stored.Data.Keys, Is.EquivalentTo(new[] { "Broker.Key" }), "The report must not add its keys to the stored broker data");
}
}
}
3 changes: 2 additions & 1 deletion src/Particular.LicensingComponent/ThroughputCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ public async Task<SignedReport> GenerateThroughputReport(string spVersion, DateT
IgnoredQueues = [.. ignoredQueueNames],
Queues = [.. queueThroughputs],
TotalQueues = queueThroughputs.Count,
EnvironmentInformation = new EnvironmentInformation { AuditServicesData = new AuditServicesData(auditServiceMetadata.Versions, auditServiceMetadata.Transports), EnvironmentData = brokerMetaData.Data }
// A copy, because the report adds its own keys. The data store can return a shared instance.
EnvironmentInformation = new EnvironmentInformation { AuditServicesData = new AuditServicesData(auditServiceMetadata.Versions, auditServiceMetadata.Transports), EnvironmentData = new Dictionary<string, string>(brokerMetaData.Data) }
};

var auditThroughput = queueThroughputs.SelectMany(w => w.DailyThroughputFromAudit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class LicensingDataStore(IServiceScopeFactory scopeFactory, TimeProvider timePro
{
static readonly string PlatformEndpointIndicator = EndpointIndicator.PlatformEndpoint.ToString();
static readonly AuditServiceMetadata DefaultAuditServiceMetadata = new([], []);
static readonly BrokerMetadata DefaultBrokerMetadata = new(null, []);

public Task<IEnumerable<Endpoint>> GetAllEndpoints(bool includePlatformEndpoints, CancellationToken cancellationToken = default) =>
ExecuteWithDbContext(async (context, token) =>
Expand Down Expand Up @@ -291,7 +290,7 @@ Task<bool> IsThereThroughput(int days, ThroughputSource? throughputSource, bool

public Task<BrokerMetadata> GetBrokerMetadata(CancellationToken cancellationToken = default) =>
ExecuteWithDbContext(async (context, token) =>
await context.GetSetting<BrokerMetadata>(SettingKeys.BrokerMetadata, token) ?? DefaultBrokerMetadata, cancellationToken);
await context.GetSetting<BrokerMetadata>(SettingKeys.BrokerMetadata, token) ?? new BrokerMetadata(null, []), cancellationToken);

public Task SaveBrokerMetadata(BrokerMetadata brokerMetadata, CancellationToken cancellationToken = default) =>
ExecuteWithDbContext((context, token) => context.StoreSetting(SettingKeys.BrokerMetadata, brokerMetadata, token), cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class LicensingDataStore(
const string LicencedEndpointDetailsDocumentId = "LicensedEndpointDetails";

static readonly AuditServiceMetadata DefaultAuditServiceMetadata = new([], []);
static readonly BrokerMetadata DefaultBrokerMetadata = new(null, []);
static readonly ReportConfigurationDocument DefaultReportConfiguration = new();

public async Task<IEnumerable<Endpoint>> GetAllEndpoints(bool includePlatformEndpoints, CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -289,7 +288,7 @@ public async Task<BrokerMetadata> GetBrokerMetadata(CancellationToken cancellati
var store = await storeProvider.GetDocumentStore(cancellationToken);
using IAsyncDocumentSession session = store.OpenAsyncSession(databaseConfiguration.Name);

return await session.LoadAsync<BrokerMetadata>(BrokerMetadataDocumentId, cancellationToken) ?? DefaultBrokerMetadata;
return await session.LoadAsync<BrokerMetadata>(BrokerMetadataDocumentId, cancellationToken) ?? new BrokerMetadata(null, []);
}

public async Task SaveBrokerMetadata(BrokerMetadata brokerMetadata, CancellationToken cancellationToken = default)
Expand Down
Loading