Skip to content
Draft
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
Expand Up @@ -10,11 +10,13 @@
using AuditThroughput;
using Contracts;
using Infrastructure;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Time.Testing;
using NuGet.Versioning;
using NUnit.Framework;
using ServiceControl.Transports.BrokerThroughput;
using Shared;

[TestFixture]
class AuditThroughputCollectorHostedService_Tests : ThroughputCollectorTestFixture
Expand All @@ -34,11 +36,19 @@ public async Task Should_handle_no_audit_remotes()
CancellationToken token = tokenSource.Token;
var fakeTimeProvider = new FakeTimeProvider();
var auditQuery = new AuditQuery_NoAuditRemotes();
var emptyConfig = new ConfigurationBuilder().Build();

using var auditThroughputCollectorHostedService = new AuditThroughputCollectorHostedService(
NullLogger<AuditThroughputCollectorHostedService>.Instance, configuration.ThroughputSettings, DataStore,
auditQuery, fakeTimeProvider)
{ DelayStart = TimeSpan.Zero };
NullLogger<AuditThroughputCollectorHostedService>.Instance,
configuration.ThroughputSettings,
DataStore,
auditQuery,
fakeTimeProvider,
new PlatformEndpointHelper(new ServiceControlSettings(emptyConfig))
)
{
DelayStart = TimeSpan.Zero
};

//Act
await auditThroughputCollectorHostedService.StartAsync(token);
Expand All @@ -64,10 +74,17 @@ public async Task Should_handle_exceptions_in_try_block_and_continue()
var fakeTimeProvider = new FakeTimeProvider();
var auditQuery = new AuditQuery_ThrowingAnExceptionOnKnownEndpointsCall();

var emptyConfig = new ConfigurationBuilder().Build();

using var auditThroughputCollectorHostedService = new AuditThroughputCollectorHostedService(
NullLogger<AuditThroughputCollectorHostedService>.Instance, configuration.ThroughputSettings, DataStore,
auditQuery, fakeTimeProvider)
{ DelayStart = TimeSpan.Zero };
auditQuery,
fakeTimeProvider,
new PlatformEndpointHelper(new ServiceControlSettings(emptyConfig))
)
{
DelayStart = TimeSpan.Zero
};

//Act
await auditThroughputCollectorHostedService.StartAsync(token);
Expand All @@ -91,11 +108,17 @@ public async Task Should_handle_cancellation_token_gracefully()
var tokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(3));
CancellationToken token = tokenSource.Token;
var fakeTimeProvider = new FakeTimeProvider();
var emptyConfig = new ConfigurationBuilder().Build();

using var auditThroughputCollectorHostedService = new AuditThroughputCollectorHostedService(
NullLogger<AuditThroughputCollectorHostedService>.Instance, configuration.ThroughputSettings, DataStore,
configuration.AuditQuery, fakeTimeProvider)
{ DelayStart = TimeSpan.Zero };
configuration.AuditQuery,
fakeTimeProvider,
new PlatformEndpointHelper(new ServiceControlSettings(emptyConfig))
)
{
DelayStart = TimeSpan.Zero
};

//Act
await auditThroughputCollectorHostedService.StartAsync(token);
Expand All @@ -116,11 +139,17 @@ public async Task Should_sanitize_endpoint_name()
string endpointName = "e$ndpoint&1";
var auditQuery = new AuditQuery_WithOneEndpoint(endpointName, 0, DateOnly.FromDateTime(DateTime.UtcNow));
string endpointNameSanitized = "e-ndpoint-1";
var emptyConfig = new ConfigurationBuilder().Build();

using var auditThroughputCollectorHostedService = new AuditThroughputCollectorHostedService(
NullLogger<AuditThroughputCollectorHostedService>.Instance, configuration.ThroughputSettings, DataStore,
auditQuery, fakeTimeProvider, new BrokerThroughputQuery_WithSanitization())
{ DelayStart = TimeSpan.Zero };
auditQuery,
fakeTimeProvider,
new PlatformEndpointHelper(new ServiceControlSettings(emptyConfig))
)
{
DelayStart = TimeSpan.Zero
};

//Act
await auditThroughputCollectorHostedService.StartAsync(token);
Expand Down Expand Up @@ -152,11 +181,16 @@ public async Task Should_not_add_the_same_endpoint_throughput_if_runs_twice_on_t
var throughputDate = DateOnly.FromDateTime(DateTime.UtcNow.AddDays(-1));
long throughputCount = 5;
var auditQuery = new AuditQuery_WithOneEndpoint(endpointName, throughputCount, throughputDate);
var emptyConfig = new ConfigurationBuilder().Build();

using var auditThroughputCollectorHostedService = new AuditThroughputCollectorHostedService(
NullLogger<AuditThroughputCollectorHostedService>.Instance, configuration.ThroughputSettings, DataStore,
auditQuery: auditQuery, fakeTimeProvider)
{ DelayStart = TimeSpan.Zero };
auditQuery: auditQuery,
fakeTimeProvider,
new PlatformEndpointHelper(new ServiceControlSettings(emptyConfig)))
{
DelayStart = TimeSpan.Zero
};

//Act
await auditThroughputCollectorHostedService.StartAsync(token1);
Expand Down Expand Up @@ -224,7 +258,11 @@ public Task<IEnumerable<ServiceControlEndpoint>> GetKnownEndpoints(CancellationT

public Task<ConnectionSettingsTestResult> TestAuditConnection(CancellationToken cancellationToken) =>
Task.FromResult(
new ConnectionSettingsTestResult { ConnectionSuccessful = true, ConnectionErrorMessages = [] });
new ConnectionSettingsTestResult
{
ConnectionSuccessful = true,
ConnectionErrorMessages = []
});

public bool InstanceParameter { get; set; }
}
Expand All @@ -245,23 +283,38 @@ public AuditQuery_WithOneEndpoint(string endpointName, long throughputCount, Dat
public Task<IEnumerable<AuditCount>> GetAuditCountForEndpoint(string endpointUrlName,
CancellationToken cancellationToken)
{
var auditCount = new AuditCount { UtcDate = ThroughputDate, Count = ThroughputCount };
var auditCount = new AuditCount
{
UtcDate = ThroughputDate,
Count = ThroughputCount
};

return Task.FromResult(new List<AuditCount> { auditCount }.AsEnumerable());
return Task.FromResult(new List<AuditCount>
{
auditCount
}.AsEnumerable());
}

public Task<List<RemoteInstanceInformation>> GetAuditRemotes(CancellationToken cancellationToken) =>
Task.FromResult<List<RemoteInstanceInformation>>([]);

public Task<IEnumerable<ServiceControlEndpoint>> GetKnownEndpoints(CancellationToken cancellationToken)
{
var scEndpoint = new ServiceControlEndpoint { Name = EndpointName, HeartbeatsEnabled = true };
var scEndpoint = new ServiceControlEndpoint
{
Name = EndpointName,
HeartbeatsEnabled = true
};
return Task.FromResult<IEnumerable<ServiceControlEndpoint>>([scEndpoint]);
}

public Task<ConnectionSettingsTestResult> TestAuditConnection(CancellationToken cancellationToken) =>
Task.FromResult(
new ConnectionSettingsTestResult { ConnectionSuccessful = true, ConnectionErrorMessages = [] });
new ConnectionSettingsTestResult
{
ConnectionSuccessful = true,
ConnectionErrorMessages = []
});

string EndpointName { get; }
long ThroughputCount { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ namespace Particular.LicensingComponent.UnitTests;
using System.Threading.Tasks;
using BrokerThroughput;
using Contracts;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Time.Testing;
using NUnit.Framework;
using Persistence.InMemory;
using ServiceControl.Transports.BrokerThroughput;
using Shared;

[TestFixture]
class BrokerThroughputCollectorHostedServiceTests
Expand All @@ -24,22 +26,36 @@ public async Task EnsuringRepeatedEndpointsSanitizedNameContainsPostfix()
var tokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(3));
CancellationToken token = tokenSource.Token;

var configuration = new ConfigurationBuilder().Build();
var dataStore = new InMemoryLicensingDataStore();

using var brokerThroughputCollectorHostedService = new BrokerThroughputCollectorHostedService(
NullLogger<BrokerThroughputCollectorHostedService>.Instance,
new MockedBrokerThroughputQuery(),
new ThroughputSettings(string.Empty, string.Empty, string.Empty, string.Empty, string.Empty),
dataStore, TimeProvider.System)
{ DelayStart = TimeSpan.Zero };
dataStore,
TimeProvider.System,
new PlatformEndpointHelper(new ServiceControlSettings(configuration)),
configuration
)
{
DelayStart = TimeSpan.Zero
};
await brokerThroughputCollectorHostedService.StartAsync(token);
await (brokerThroughputCollectorHostedService.ExecuteTask! ?? Task.CompletedTask);

Endpoint[] endpoints = (await dataStore.GetAllEndpoints(true, token)).ToArray();
IEnumerable<string> sanitizedNames = endpoints.Select(endpoint => endpoint.SanitizedName);
IEnumerable<string> queueNames = endpoints.Select(endpoint => endpoint.Id.Name);

Assert.That(sanitizedNames, Is.EquivalentTo(new[] { "sales", "sales1", "marketing", "customer" }));
Assert.That(queueNames, Is.EquivalentTo(new[] { "sales@one", "sales@two", "marketing", "customer" }));
Assert.That(sanitizedNames, Is.EquivalentTo(new[]
{
"sales", "sales1", "marketing", "customer"
}));
Assert.That(queueNames, Is.EquivalentTo(new[]
{
"sales@one", "sales@two", "marketing", "customer"
}));
}

[Test]
Expand All @@ -57,12 +73,20 @@ await dataStore.RecordEndpointThroughput("marketing", ThroughputSource.Broker,
await dataStore.RecordEndpointThroughput("customer", ThroughputSource.Broker,
new List<EndpointDailyThroughput>([new EndpointDailyThroughput(lastCollectionDate, 100)]), token);
var mockedBrokerThroughputQueryThatRecordsDate = new MockedBrokerThroughputQueryThatRecordsDate();
var emptyConfig = new ConfigurationBuilder().Build();

using var brokerThroughputCollectorHostedService = new BrokerThroughputCollectorHostedService(
NullLogger<BrokerThroughputCollectorHostedService>.Instance,
mockedBrokerThroughputQueryThatRecordsDate,
new ThroughputSettings(string.Empty, string.Empty, string.Empty, string.Empty, string.Empty),
dataStore, TimeProvider.System)
{ DelayStart = TimeSpan.Zero };
dataStore,
TimeProvider.System,
new PlatformEndpointHelper(new ServiceControlSettings(emptyConfig)),
emptyConfig
)
{
DelayStart = TimeSpan.Zero
};
await brokerThroughputCollectorHostedService.StartAsync(token);
await (brokerThroughputCollectorHostedService.ExecuteTask! ?? Task.CompletedTask);

Expand All @@ -81,12 +105,20 @@ public async Task EnsuringExceptionsAreHandledAndThroughputCollectorKeepsGoing()
var dataStore = new InMemoryLicensingDataStore();
var fakeTimeProvider = new FakeTimeProvider();
var mockedBrokerThroughputQueryThatThrowsExceptions = new MockedBrokerThroughputQueryThatThrowsExceptions();
var emptyConfig = new ConfigurationBuilder().Build();

using var brokerThroughputCollectorHostedService = new BrokerThroughputCollectorHostedService(
NullLogger<BrokerThroughputCollectorHostedService>.Instance,
mockedBrokerThroughputQueryThatThrowsExceptions,
new ThroughputSettings(string.Empty, string.Empty, string.Empty, string.Empty, string.Empty),
dataStore, fakeTimeProvider)
{ DelayStart = TimeSpan.Zero };
dataStore,
fakeTimeProvider,
new PlatformEndpointHelper(new ServiceControlSettings(emptyConfig)),
emptyConfig
)
{
DelayStart = TimeSpan.Zero
};
await brokerThroughputCollectorHostedService.StartAsync(token);

await Task.Run(async () =>
Expand All @@ -112,12 +144,20 @@ public async Task EnsuringHostedServiceStopCleanly()
CancellationToken token = tokenSource.Token;

var dataStore = new InMemoryLicensingDataStore();
var emptyConfig = new ConfigurationBuilder().Build();

using var brokerThroughputCollectorHostedService = new BrokerThroughputCollectorHostedService(
NullLogger<BrokerThroughputCollectorHostedService>.Instance,
new MockedBrokerThroughputQuery(),
new ThroughputSettings(string.Empty, string.Empty, string.Empty, string.Empty, string.Empty),
dataStore, TimeProvider.System)
{ DelayStart = TimeSpan.Zero };
dataStore,
TimeProvider.System,
new PlatformEndpointHelper(new ServiceControlSettings(emptyConfig)),
emptyConfig
)
{
DelayStart = TimeSpan.Zero
};
await brokerThroughputCollectorHostedService.StartAsync(token);
await Task.Delay(TimeSpan.FromSeconds(2), token);
await brokerThroughputCollectorHostedService.StopAsync(token);
Expand Down Expand Up @@ -197,8 +237,14 @@ public async IAsyncEnumerable<QueueThroughput> GetThroughputPerDay(IBrokerQueue
public async IAsyncEnumerable<IBrokerQueue> GetQueueNames(
[EnumeratorCancellation] CancellationToken cancellationToken)
{
yield return new DefaultBrokerQueue("sales@one") { SanitizedName = "sales" };
yield return new DefaultBrokerQueue("sales@two") { SanitizedName = "sales" };
yield return new DefaultBrokerQueue("sales@one")
{
SanitizedName = "sales"
};
yield return new DefaultBrokerQueue("sales@two")
{
SanitizedName = "sales"
};
yield return new DefaultBrokerQueue("marketing");
yield return new DefaultBrokerQueue("customer");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ namespace Particular.LicensingComponent.UnitTests;
using Approvals;
using Contracts;
using Infrastructure;
using Microsoft.Extensions.Configuration;
using MonitoringThroughput;
using NUnit.Framework;
using ServiceControl.Transports.BrokerThroughput;
using Shared;

[TestFixture]
class MonitoringService_Tests : ThroughputCollectorTestFixture
Expand Down Expand Up @@ -82,15 +84,21 @@ public async Task Should_sanitize_endpoint_name()
EndpointThroughputData = new EndpointThroughputData[] { new() { Name = endpointName, Throughput = 15 } }
};

var monitoringService = new MonitoringService(DataStore, new BrokerThroughputQuery_WithSanitization());
var emptyConfig = new ConfigurationBuilder().Build();

var monitoringService = new MonitoringService(
DataStore,
new ServiceControlSettings(emptyConfig),
new BrokerThroughputQuery_WithSanitization()
);
byte[] messageBytes = JsonSerializer.SerializeToUtf8Bytes(message);
await monitoringService.RecordMonitoringThroughput(messageBytes, default);
string endpointNameSanitized = "e-ndpoint-1";

// Act
Endpoint foundEndpoint = await DataStore.GetEndpoint(endpointName, ThroughputSource.Monitoring, default);

// Assert
// Assert
Assert.That(foundEndpoint, Is.Not.Null, $"Expected endpoint {endpointName} not found.");
Assert.That(foundEndpoint.SanitizedName, Is.EqualTo(endpointNameSanitized),
$"Endpoint {endpointName} name not sanitized correctly.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public class AuditThroughputCollectorHostedService(
ILicensingDataStore dataStore,
IAuditQuery auditQuery,
TimeProvider timeProvider,
IBrokerThroughputQuery? brokerThroughputQuery = null) : BackgroundService
PlatformEndpointHelper platformEndpointHelper,
IBrokerThroughputQuery? brokerThroughputQuery = null
) : BackgroundService
{
public TimeSpan DelayStart { get; set; } = TimeSpan.FromSeconds(40);
public static List<string> AuditQueues { get; set; } = [];
Expand Down Expand Up @@ -98,7 +100,7 @@ Endpoint ConvertToEndpoint(ServiceControlEndpoint scEndpoint)
EndpointIndicators = [EndpointIndicator.KnownEndpoint.ToString()]
};

if (PlatformEndpointHelper.IsPlatformEndpoint(scEndpoint.Name, throughputSettings))
if (platformEndpointHelper.IsPlatformEndpoint(scEndpoint.Name, throughputSettings))
{
endpoint.EndpointIndicators.Append(EndpointIndicator.PlatformEndpoint.ToString());
}
Expand Down
Loading
Loading