Skip to content

Commit 1b7510e

Browse files
committed
wip, loaded test projects again and started updating broken tests. Also converted RavenPersistenceConfiguration into a IConfigureOptions<RavenPersisterSettings> and added IValidateOptions for some audit persister components
1 parent d228153 commit 1b7510e

File tree

34 files changed

+574
-308
lines changed

34 files changed

+574
-308
lines changed

src/Particular.LicensingComponent.UnitTests/AuditThroughputCollectorHostedService_Tests.cs

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
using AuditThroughput;
1111
using Contracts;
1212
using Infrastructure;
13+
using Microsoft.Extensions.Configuration;
1314
using Microsoft.Extensions.Logging.Abstractions;
1415
using Microsoft.Extensions.Time.Testing;
1516
using NuGet.Versioning;
1617
using NUnit.Framework;
1718
using ServiceControl.Transports.BrokerThroughput;
19+
using Shared;
1820

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

3841
using var auditThroughputCollectorHostedService = new AuditThroughputCollectorHostedService(
39-
NullLogger<AuditThroughputCollectorHostedService>.Instance, configuration.ThroughputSettings, DataStore,
40-
auditQuery, fakeTimeProvider)
41-
{ DelayStart = TimeSpan.Zero };
42+
NullLogger<AuditThroughputCollectorHostedService>.Instance,
43+
configuration.ThroughputSettings,
44+
DataStore,
45+
auditQuery,
46+
fakeTimeProvider,
47+
new PlatformEndpointHelper(new ServiceControlSettings(emptyConfig))
48+
)
49+
{
50+
DelayStart = TimeSpan.Zero
51+
};
4252

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

77+
var emptyConfig = new ConfigurationBuilder().Build();
78+
6779
using var auditThroughputCollectorHostedService = new AuditThroughputCollectorHostedService(
6880
NullLogger<AuditThroughputCollectorHostedService>.Instance, configuration.ThroughputSettings, DataStore,
69-
auditQuery, fakeTimeProvider)
70-
{ DelayStart = TimeSpan.Zero };
81+
auditQuery,
82+
fakeTimeProvider,
83+
new PlatformEndpointHelper(new ServiceControlSettings(emptyConfig))
84+
)
85+
{
86+
DelayStart = TimeSpan.Zero
87+
};
7188

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

95113
using var auditThroughputCollectorHostedService = new AuditThroughputCollectorHostedService(
96114
NullLogger<AuditThroughputCollectorHostedService>.Instance, configuration.ThroughputSettings, DataStore,
97-
configuration.AuditQuery, fakeTimeProvider)
98-
{ DelayStart = TimeSpan.Zero };
115+
configuration.AuditQuery,
116+
fakeTimeProvider,
117+
new PlatformEndpointHelper(new ServiceControlSettings(emptyConfig))
118+
)
119+
{
120+
DelayStart = TimeSpan.Zero
121+
};
99122

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

120144
using var auditThroughputCollectorHostedService = new AuditThroughputCollectorHostedService(
121145
NullLogger<AuditThroughputCollectorHostedService>.Instance, configuration.ThroughputSettings, DataStore,
122-
auditQuery, fakeTimeProvider, new BrokerThroughputQuery_WithSanitization())
123-
{ DelayStart = TimeSpan.Zero };
146+
auditQuery,
147+
fakeTimeProvider,
148+
new PlatformEndpointHelper(new ServiceControlSettings(emptyConfig))
149+
)
150+
{
151+
DelayStart = TimeSpan.Zero
152+
};
124153

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

156186
using var auditThroughputCollectorHostedService = new AuditThroughputCollectorHostedService(
157187
NullLogger<AuditThroughputCollectorHostedService>.Instance, configuration.ThroughputSettings, DataStore,
158-
auditQuery: auditQuery, fakeTimeProvider)
159-
{ DelayStart = TimeSpan.Zero };
188+
auditQuery: auditQuery,
189+
fakeTimeProvider,
190+
new PlatformEndpointHelper(new ServiceControlSettings(emptyConfig)))
191+
{
192+
DelayStart = TimeSpan.Zero
193+
};
160194

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

225259
public Task<ConnectionSettingsTestResult> TestAuditConnection(CancellationToken cancellationToken) =>
226260
Task.FromResult(
227-
new ConnectionSettingsTestResult { ConnectionSuccessful = true, ConnectionErrorMessages = [] });
261+
new ConnectionSettingsTestResult
262+
{
263+
ConnectionSuccessful = true,
264+
ConnectionErrorMessages = []
265+
});
228266

229267
public bool InstanceParameter { get; set; }
230268
}
@@ -245,23 +283,38 @@ public AuditQuery_WithOneEndpoint(string endpointName, long throughputCount, Dat
245283
public Task<IEnumerable<AuditCount>> GetAuditCountForEndpoint(string endpointUrlName,
246284
CancellationToken cancellationToken)
247285
{
248-
var auditCount = new AuditCount { UtcDate = ThroughputDate, Count = ThroughputCount };
286+
var auditCount = new AuditCount
287+
{
288+
UtcDate = ThroughputDate,
289+
Count = ThroughputCount
290+
};
249291

250-
return Task.FromResult(new List<AuditCount> { auditCount }.AsEnumerable());
292+
return Task.FromResult(new List<AuditCount>
293+
{
294+
auditCount
295+
}.AsEnumerable());
251296
}
252297

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

256301
public Task<IEnumerable<ServiceControlEndpoint>> GetKnownEndpoints(CancellationToken cancellationToken)
257302
{
258-
var scEndpoint = new ServiceControlEndpoint { Name = EndpointName, HeartbeatsEnabled = true };
303+
var scEndpoint = new ServiceControlEndpoint
304+
{
305+
Name = EndpointName,
306+
HeartbeatsEnabled = true
307+
};
259308
return Task.FromResult<IEnumerable<ServiceControlEndpoint>>([scEndpoint]);
260309
}
261310

262311
public Task<ConnectionSettingsTestResult> TestAuditConnection(CancellationToken cancellationToken) =>
263312
Task.FromResult(
264-
new ConnectionSettingsTestResult { ConnectionSuccessful = true, ConnectionErrorMessages = [] });
313+
new ConnectionSettingsTestResult
314+
{
315+
ConnectionSuccessful = true,
316+
ConnectionErrorMessages = []
317+
});
265318

266319
string EndpointName { get; }
267320
long ThroughputCount { get; }

src/Particular.LicensingComponent.UnitTests/BrokerThroughputCollectorHostedServiceTests.cs

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ namespace Particular.LicensingComponent.UnitTests;
99
using System.Threading.Tasks;
1010
using BrokerThroughput;
1111
using Contracts;
12+
using Microsoft.Extensions.Configuration;
1213
using Microsoft.Extensions.Logging.Abstractions;
1314
using Microsoft.Extensions.Time.Testing;
1415
using NUnit.Framework;
1516
using Persistence.InMemory;
1617
using ServiceControl.Transports.BrokerThroughput;
18+
using Shared;
1719

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

29+
var configuration = new ConfigurationBuilder().Build();
2730
var dataStore = new InMemoryLicensingDataStore();
31+
2832
using var brokerThroughputCollectorHostedService = new BrokerThroughputCollectorHostedService(
2933
NullLogger<BrokerThroughputCollectorHostedService>.Instance,
3034
new MockedBrokerThroughputQuery(),
3135
new ThroughputSettings(string.Empty, string.Empty, string.Empty, string.Empty, string.Empty),
32-
dataStore, TimeProvider.System)
33-
{ DelayStart = TimeSpan.Zero };
36+
dataStore,
37+
TimeProvider.System,
38+
new PlatformEndpointHelper(new ServiceControlSettings(configuration)),
39+
configuration
40+
)
41+
{
42+
DelayStart = TimeSpan.Zero
43+
};
3444
await brokerThroughputCollectorHostedService.StartAsync(token);
3545
await (brokerThroughputCollectorHostedService.ExecuteTask! ?? Task.CompletedTask);
3646

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

41-
Assert.That(sanitizedNames, Is.EquivalentTo(new[] { "sales", "sales1", "marketing", "customer" }));
42-
Assert.That(queueNames, Is.EquivalentTo(new[] { "sales@one", "sales@two", "marketing", "customer" }));
51+
Assert.That(sanitizedNames, Is.EquivalentTo(new[]
52+
{
53+
"sales", "sales1", "marketing", "customer"
54+
}));
55+
Assert.That(queueNames, Is.EquivalentTo(new[]
56+
{
57+
"sales@one", "sales@two", "marketing", "customer"
58+
}));
4359
}
4460

4561
[Test]
@@ -57,12 +73,20 @@ await dataStore.RecordEndpointThroughput("marketing", ThroughputSource.Broker,
5773
await dataStore.RecordEndpointThroughput("customer", ThroughputSource.Broker,
5874
new List<EndpointDailyThroughput>([new EndpointDailyThroughput(lastCollectionDate, 100)]), token);
5975
var mockedBrokerThroughputQueryThatRecordsDate = new MockedBrokerThroughputQueryThatRecordsDate();
76+
var emptyConfig = new ConfigurationBuilder().Build();
77+
6078
using var brokerThroughputCollectorHostedService = new BrokerThroughputCollectorHostedService(
6179
NullLogger<BrokerThroughputCollectorHostedService>.Instance,
6280
mockedBrokerThroughputQueryThatRecordsDate,
6381
new ThroughputSettings(string.Empty, string.Empty, string.Empty, string.Empty, string.Empty),
64-
dataStore, TimeProvider.System)
65-
{ DelayStart = TimeSpan.Zero };
82+
dataStore,
83+
TimeProvider.System,
84+
new PlatformEndpointHelper(new ServiceControlSettings(emptyConfig)),
85+
emptyConfig
86+
)
87+
{
88+
DelayStart = TimeSpan.Zero
89+
};
6690
await brokerThroughputCollectorHostedService.StartAsync(token);
6791
await (brokerThroughputCollectorHostedService.ExecuteTask! ?? Task.CompletedTask);
6892

@@ -81,12 +105,20 @@ public async Task EnsuringExceptionsAreHandledAndThroughputCollectorKeepsGoing()
81105
var dataStore = new InMemoryLicensingDataStore();
82106
var fakeTimeProvider = new FakeTimeProvider();
83107
var mockedBrokerThroughputQueryThatThrowsExceptions = new MockedBrokerThroughputQueryThatThrowsExceptions();
108+
var emptyConfig = new ConfigurationBuilder().Build();
109+
84110
using var brokerThroughputCollectorHostedService = new BrokerThroughputCollectorHostedService(
85111
NullLogger<BrokerThroughputCollectorHostedService>.Instance,
86112
mockedBrokerThroughputQueryThatThrowsExceptions,
87113
new ThroughputSettings(string.Empty, string.Empty, string.Empty, string.Empty, string.Empty),
88-
dataStore, fakeTimeProvider)
89-
{ DelayStart = TimeSpan.Zero };
114+
dataStore,
115+
fakeTimeProvider,
116+
new PlatformEndpointHelper(new ServiceControlSettings(emptyConfig)),
117+
emptyConfig
118+
)
119+
{
120+
DelayStart = TimeSpan.Zero
121+
};
90122
await brokerThroughputCollectorHostedService.StartAsync(token);
91123

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

114146
var dataStore = new InMemoryLicensingDataStore();
147+
var emptyConfig = new ConfigurationBuilder().Build();
148+
115149
using var brokerThroughputCollectorHostedService = new BrokerThroughputCollectorHostedService(
116150
NullLogger<BrokerThroughputCollectorHostedService>.Instance,
117151
new MockedBrokerThroughputQuery(),
118152
new ThroughputSettings(string.Empty, string.Empty, string.Empty, string.Empty, string.Empty),
119-
dataStore, TimeProvider.System)
120-
{ DelayStart = TimeSpan.Zero };
153+
dataStore,
154+
TimeProvider.System,
155+
new PlatformEndpointHelper(new ServiceControlSettings(emptyConfig)),
156+
emptyConfig
157+
)
158+
{
159+
DelayStart = TimeSpan.Zero
160+
};
121161
await brokerThroughputCollectorHostedService.StartAsync(token);
122162
await Task.Delay(TimeSpan.FromSeconds(2), token);
123163
await brokerThroughputCollectorHostedService.StopAsync(token);
@@ -197,8 +237,14 @@ public async IAsyncEnumerable<QueueThroughput> GetThroughputPerDay(IBrokerQueue
197237
public async IAsyncEnumerable<IBrokerQueue> GetQueueNames(
198238
[EnumeratorCancellation] CancellationToken cancellationToken)
199239
{
200-
yield return new DefaultBrokerQueue("sales@one") { SanitizedName = "sales" };
201-
yield return new DefaultBrokerQueue("sales@two") { SanitizedName = "sales" };
240+
yield return new DefaultBrokerQueue("sales@one")
241+
{
242+
SanitizedName = "sales"
243+
};
244+
yield return new DefaultBrokerQueue("sales@two")
245+
{
246+
SanitizedName = "sales"
247+
};
202248
yield return new DefaultBrokerQueue("marketing");
203249
yield return new DefaultBrokerQueue("customer");
204250

src/Particular.LicensingComponent.UnitTests/MonitoringService_Tests.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ namespace Particular.LicensingComponent.UnitTests;
1111
using Approvals;
1212
using Contracts;
1313
using Infrastructure;
14+
using Microsoft.Extensions.Configuration;
1415
using MonitoringThroughput;
1516
using NUnit.Framework;
1617
using ServiceControl.Transports.BrokerThroughput;
18+
using Shared;
1719

1820
[TestFixture]
1921
class MonitoringService_Tests : ThroughputCollectorTestFixture
@@ -82,15 +84,21 @@ public async Task Should_sanitize_endpoint_name()
8284
EndpointThroughputData = new EndpointThroughputData[] { new() { Name = endpointName, Throughput = 15 } }
8385
};
8486

85-
var monitoringService = new MonitoringService(DataStore, new BrokerThroughputQuery_WithSanitization());
87+
var emptyConfig = new ConfigurationBuilder().Build();
88+
89+
var monitoringService = new MonitoringService(
90+
DataStore,
91+
new ServiceControlSettings(emptyConfig),
92+
new BrokerThroughputQuery_WithSanitization()
93+
);
8694
byte[] messageBytes = JsonSerializer.SerializeToUtf8Bytes(message);
8795
await monitoringService.RecordMonitoringThroughput(messageBytes, default);
8896
string endpointNameSanitized = "e-ndpoint-1";
8997

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

93-
// Assert
101+
// Assert
94102
Assert.That(foundEndpoint, Is.Not.Null, $"Expected endpoint {endpointName} not found.");
95103
Assert.That(foundEndpoint.SanitizedName, Is.EqualTo(endpointNameSanitized),
96104
$"Endpoint {endpointName} name not sanitized correctly.");

src/Particular.LicensingComponent/Shared/ServiceControlSettings.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
using Microsoft.Extensions.Configuration;
44
using Particular.LicensingComponent.Contracts;
55

6-
public class ServiceControlSettings (IConfiguration configuration)
6+
public class ServiceControlSettings(IConfiguration configuration)
77
{
88
public const string MessageTransport = "ServiceControl";
99
public const string ServiceControlThroughputDataQueueSetting = "ServiceControlThroughputDataQueue";
1010
public string ServiceControlThroughputDataQueue => configuration
1111
.GetSection(ThroughputSettings.SettingsNamespace.Root)
12-
.GetValue<string>(ServiceControlThroughputDataQueueSetting, "ServiceControl.ThroughputData");
12+
.GetValue<string>(ServiceControlThroughputDataQueueSetting, "ServiceControl.ThroughputData") ?? "ServiceControl.ThroughputData";
1313

1414
static string MonitoringQueue = $"Monitoring/{ServiceControlThroughputDataQueueSetting}";
1515
static string MonitoringQueueDescription = "Queue to send monitoring throughput data to for processing by ServiceControl. This setting only needs to be specified if the Monitoring instance is not hosted in the same machine as the Error instance is running on.";

0 commit comments

Comments
 (0)