Skip to content

Commit cd62b02

Browse files
committed
Remove usage of RegisterChangeCallback and related in tests
1 parent 2b57094 commit cd62b02

File tree

2 files changed

+6
-41
lines changed

2 files changed

+6
-41
lines changed

src/Common/src/Certificates/ConfigureCertificateOptions.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using System.Text.RegularExpressions;
99
using Microsoft.Extensions.Configuration;
1010
using Microsoft.Extensions.Logging;
11-
using Microsoft.Extensions.Logging.Abstractions;
1211
using Microsoft.Extensions.Options;
1312

1413
namespace Steeltoe.Common.Certificates;
@@ -63,8 +62,7 @@ public void Configure(string? name, CertificateOptions options)
6362
}
6463
catch (IOException ex)
6564
{
66-
_logger.LogDebug(ex, "Failed to load certificate for '{CertificateName}' from '{Path}'. Will retry on next reload.", name,
67-
certificateFilePath);
65+
_logger.LogDebug(ex, "Failed to load certificate for '{CertificateName}' from '{Path}'. Will retry on next reload.", name, certificateFilePath);
6866
}
6967
catch (CryptographicException ex)
7068
{

src/Common/test/Certificates.Test/ConfigureCertificateOptionsTest.cs

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using Microsoft.Extensions.Logging;
1111
using Microsoft.Extensions.Logging.Abstractions;
1212
using Microsoft.Extensions.Options;
13-
using Microsoft.Extensions.Primitives;
1413
using Steeltoe.Common.TestResources;
1514
using Steeltoe.Common.TestResources.IO;
1615

@@ -187,33 +186,13 @@ public async Task CertificateOptions_update_on_changed_contents(string certifica
187186

188187
var optionsMonitor = serviceProvider.GetRequiredService<IOptionsMonitor<CertificateOptions>>();
189188
optionsMonitor.Get(certificateName).Certificate.Should().BeEquivalentTo(firstX509);
190-
IOptionsChangeTokenSource<CertificateOptions>[] tokenSources = [.. serviceProvider.GetServices<IOptionsChangeTokenSource<CertificateOptions>>()];
191-
192-
List<FilePathInOptionsChangeTokenSource<CertificateOptions>> matchingTokenSources =
193-
[
194-
.. tokenSources.OfType<FilePathInOptionsChangeTokenSource<CertificateOptions>>()
195-
];
196-
197-
matchingTokenSources.Should().HaveCount(2);
198-
199-
List<IChangeToken> changeTokens = [.. matchingTokenSources.Select(s => s.GetChangeToken())];
200-
201-
bool certificateContentsChangeCalled = false;
202-
bool keyContentsChangeCalled = false;
203-
using IDisposable certificateContentChangeToken = changeTokens[0].RegisterChangeCallback(_ => certificateContentsChangeCalled = true, null);
204-
using IDisposable keyContentChangeToken = changeTokens[1].RegisterChangeCallback(_ => keyContentsChangeCalled = true, null);
205-
206-
certificateContentsChangeCalled.Should().BeFalse("certificate file content has not changed yet");
207-
keyContentsChangeCalled.Should().BeFalse("key file content has not changed yet");
208189

209190
await File.WriteAllTextAsync(certificateFilePath, secondCertificateContent, TestContext.Current.CancellationToken);
210191
await File.WriteAllTextAsync(privateKeyFilePath, secondPrivateKeyContent, TestContext.Current.CancellationToken);
211192

212193
using Task pollTask = WaitUntilCertificateChangedToAsync(secondX509, optionsMonitor, certificateName, TestContext.Current.CancellationToken);
213194
await pollTask.WaitAsync(TimeSpan.FromSeconds(4), TestContext.Current.CancellationToken);
214195

215-
certificateContentsChangeCalled.Should().BeTrue("certificate file contents changed");
216-
keyContentsChangeCalled.Should().BeTrue("key file contents changed");
217196
optionsMonitor.Get(certificateName).Certificate.Should().Be(secondX509);
218197
}
219198

@@ -244,26 +223,14 @@ public async Task CertificateOptions_update_on_changed_path(string certificateNa
244223

245224
var optionsMonitor = serviceProvider.GetRequiredService<IOptionsMonitor<CertificateOptions>>();
246225
optionsMonitor.Get(certificateName).Certificate.Should().BeEquivalentTo(firstX509);
247-
IOptionsChangeTokenSource<CertificateOptions>[] tokenSources = [.. serviceProvider.GetServices<IOptionsChangeTokenSource<CertificateOptions>>()];
248-
249-
IChangeToken configurationChangeToken =
250-
tokenSources.OfType<ConfigurationChangeTokenSource<CertificateOptions>>().Should().ContainSingle().Which.GetChangeToken();
251226

252-
bool configurationChangeCalled = false;
227+
appSettings = BuildAppSettingsJson(certificateName, "secondInstance.crt", "secondInstance.key");
228+
await File.WriteAllTextAsync(appSettingsPath, appSettings, TestContext.Current.CancellationToken);
253229

254-
using (configurationChangeToken.RegisterChangeCallback(_ => configurationChangeCalled = true, null))
255-
{
256-
configurationChangeCalled.Should().BeFalse("file path information has not changed yet");
257-
258-
appSettings = BuildAppSettingsJson(certificateName, "secondInstance.crt", "secondInstance.key");
259-
await File.WriteAllTextAsync(appSettingsPath, appSettings, TestContext.Current.CancellationToken);
260-
261-
using Task pollTask = WaitUntilCertificateChangedToAsync(secondX509, optionsMonitor, certificateName, TestContext.Current.CancellationToken);
262-
await pollTask.WaitAsync(TimeSpan.FromSeconds(4), TestContext.Current.CancellationToken);
230+
using Task pollTask = WaitUntilCertificateChangedToAsync(secondX509, optionsMonitor, certificateName, TestContext.Current.CancellationToken);
231+
await pollTask.WaitAsync(TimeSpan.FromSeconds(4), TestContext.Current.CancellationToken);
263232

264-
configurationChangeCalled.Should().BeTrue("file path information changed");
265-
optionsMonitor.Get(certificateName).Certificate.Should().Be(secondX509);
266-
}
233+
optionsMonitor.Get(certificateName).Certificate.Should().Be(secondX509);
267234
}
268235

269236
private static string BuildAppSettingsJson(string certificateName, string certificatePath, string keyPath)

0 commit comments

Comments
 (0)