From 7a1c224e1a757fc5e0d5315db59c87dfae04baf1 Mon Sep 17 00:00:00 2001 From: Martijn Bodeman <11424653+skwasjer@users.noreply.github.com> Date: Sun, 6 Oct 2024 09:33:00 +0200 Subject: [PATCH] style: apply (new) code style (#54) --- .../Properties/InternalsVisibleTo.cs | 2 +- .../BackgroundTestExecutor.cs | 7 +- .../GlobalConfigurationExtensionsTests.cs | 2 +- .../GlobalTestContext.cs | 11 +- .../HangfireIntegrationTests.cs | 125 +++++++++++++----- .../HangfireServiceProviderTests.cs | 31 +++-- 6 files changed, 120 insertions(+), 58 deletions(-) diff --git a/src/Hangfire.Correlate/Properties/InternalsVisibleTo.cs b/src/Hangfire.Correlate/Properties/InternalsVisibleTo.cs index 453e7d3..773ca4d 100644 --- a/src/Hangfire.Correlate/Properties/InternalsVisibleTo.cs +++ b/src/Hangfire.Correlate/Properties/InternalsVisibleTo.cs @@ -1,3 +1,3 @@ using System.Runtime.CompilerServices; -[assembly: InternalsVisibleTo("Hangfire.Correlate.Tests")] \ No newline at end of file +[assembly: InternalsVisibleTo("Hangfire.Correlate.Tests")] diff --git a/test/Hangfire.Correlate.Tests/BackgroundTestExecutor.cs b/test/Hangfire.Correlate.Tests/BackgroundTestExecutor.cs index ab42eb9..effc29c 100644 --- a/test/Hangfire.Correlate.Tests/BackgroundTestExecutor.cs +++ b/test/Hangfire.Correlate.Tests/BackgroundTestExecutor.cs @@ -14,7 +14,12 @@ internal BackgroundTestExecutor() { } - public BackgroundTestExecutor(TestService testService, ICorrelationContextAccessor correlationContextAccessor, ICollection jobState, ITestOutputHelper testOutputHelper) + public BackgroundTestExecutor( + TestService testService, + ICorrelationContextAccessor correlationContextAccessor, + ICollection jobState, + ITestOutputHelper testOutputHelper + ) { _testService = testService ?? throw new ArgumentNullException(nameof(testService)); _correlationContextAccessor = correlationContextAccessor ?? throw new ArgumentNullException(nameof(correlationContextAccessor)); diff --git a/test/Hangfire.Correlate.Tests/GlobalConfigurationExtensionsTests.cs b/test/Hangfire.Correlate.Tests/GlobalConfigurationExtensionsTests.cs index cc9a537..e2f3747 100644 --- a/test/Hangfire.Correlate.Tests/GlobalConfigurationExtensionsTests.cs +++ b/test/Hangfire.Correlate.Tests/GlobalConfigurationExtensionsTests.cs @@ -78,7 +78,7 @@ public void Given_that_required_services_are_not_registered_when_using_it_should { Action act = () => { - using ServiceProvider? services = new ServiceCollection() + using ServiceProvider services = new ServiceCollection() .BuildServiceProvider(); _configMock.UseCorrelate(services); }; diff --git a/test/Hangfire.Correlate.Tests/GlobalTestContext.cs b/test/Hangfire.Correlate.Tests/GlobalTestContext.cs index 37ed2e8..096bb9a 100644 --- a/test/Hangfire.Correlate.Tests/GlobalTestContext.cs +++ b/test/Hangfire.Correlate.Tests/GlobalTestContext.cs @@ -1,7 +1,4 @@ -using Hangfire.Common; -using Xunit; - -namespace Hangfire.Correlate; +namespace Hangfire.Correlate; /// /// Marker to disable parallel tests because when registering Hangfire filters, it adds it to a static list. @@ -24,9 +21,9 @@ public virtual Task DisposeAsync() private static void CleanUpFilters() { foreach (object? filter in GlobalJobFilters.Filters - .Where(f => f.Instance is CorrelateFilterAttribute) - .Select(f => f.Instance) - .ToArray()) + .Where(f => f.Instance is CorrelateFilterAttribute) + .Select(f => f.Instance) + .ToArray()) { GlobalJobFilters.Filters.Remove(filter); } diff --git a/test/Hangfire.Correlate.Tests/HangfireIntegrationTests.cs b/test/Hangfire.Correlate.Tests/HangfireIntegrationTests.cs index b7099fe..2ee17a8 100644 --- a/test/Hangfire.Correlate.Tests/HangfireIntegrationTests.cs +++ b/test/Hangfire.Correlate.Tests/HangfireIntegrationTests.cs @@ -72,8 +72,8 @@ private async Task WaitUntilJobCompletedAsync(string jobId, int maxWaitInMillise var sw = Stopwatch.StartNew(); StateHistoryDto[] jobHistory = Array.Empty(); while ( - (jobHistory.All(s => s.StateName != "Succeeded")) - && (sw.Elapsed.TotalMilliseconds < maxWaitInMilliseconds || Debugger.IsAttached)) + jobHistory.All(s => s.StateName != "Succeeded") + && (sw.Elapsed.TotalMilliseconds < maxWaitInMilliseconds || Debugger.IsAttached)) { await Task.Delay(25); JobDetailsDto jobDetails = monitoringApi.JobDetails(jobId); @@ -109,12 +109,14 @@ public async Task Given_job_is_queued_inside_correlationContext_should_put_corre .Verifiable(); // Act - await _correlationManager.CorrelateAsync(correlationId, + await _correlationManager.CorrelateAsync( + correlationId, async () => { await Task.Yield(); jobId = _client.Enqueue(job => job.RunAsync(TimeoutInMillis, null!)); - }); + } + ); while (true) { @@ -123,6 +125,7 @@ await _correlationManager.CorrelateAsync(correlationId, break; } } + await WaitUntilJobCompletedAsync(jobId.Should().NotBeNull().And.Subject); // Assert @@ -133,22 +136,31 @@ await _correlationManager.CorrelateAsync(correlationId, public async Task Given_job_is_queued_inside_correlationContext_should_use_correlationId_of_correlation_context() { const string correlationId = "my-id"; - var expectedJob = new BackgroundTestExecutor { CorrelationId = correlationId, JobHasCompleted = true }; + var expectedJob = new BackgroundTestExecutor + { + CorrelationId = correlationId, + JobHasCompleted = true + }; // Act - await _correlationManager.CorrelateAsync(correlationId, + await _correlationManager.CorrelateAsync( + correlationId, async () => { await Task.Yield(); expectedJob.JobId = _client.Enqueue(job => job.RunAsync(TimeoutInMillis, null!)); - }); + } + ); await WaitUntilJobCompletedAsync(expectedJob.JobId); // Assert ExecutedJobs.Should() .BeEquivalentTo( - new List { expectedJob }, + new List + { + expectedJob + }, "a correlation context exists, so the correlation id should be used when performing the job" ); } @@ -188,21 +200,30 @@ public async Task Given_job_is_queued_outside_correlationContext_should_use_jobI // Assert ExecutedJobs.Should() .BeEquivalentTo( - new List { expectedJob }, + new List + { + expectedJob + }, "no correlation context exists, so the job id should be used when performing the job" ); } [Fact] - public async Task Given_parent_job_is_queued_outside_correlation_context_when_queueing_continuation_also_outside_context_should_use_job_id_from_parent() + public async Task + Given_parent_job_is_queued_outside_correlation_context_when_queueing_continuation_also_outside_context_should_use_job_id_from_parent() { - var expectedParentJob = new BackgroundTestExecutor { JobHasCompleted = true }; - var expectedContinuationJob = new BackgroundTestExecutor { JobHasCompleted = true }; + var expectedParentJob = new BackgroundTestExecutor + { + JobHasCompleted = true + }; + var expectedContinuationJob = new BackgroundTestExecutor + { + JobHasCompleted = true + }; // Queue parent - expectedParentJob.JobId = expectedParentJob.CorrelationId = expectedContinuationJob.CorrelationId = _client.Enqueue( - job => job.RunAsync(TimeoutInMillis, null!) - ); + expectedParentJob.JobId = expectedParentJob.CorrelationId = expectedContinuationJob.CorrelationId = + _client.Enqueue(job => job.RunAsync(TimeoutInMillis, null!)); // Act expectedContinuationJob.JobId = _client.ContinueJobWith( @@ -215,61 +236,89 @@ public async Task Given_parent_job_is_queued_outside_correlation_context_when_qu // Assert ExecutedJobs.Should() .BeEquivalentTo( - new List { expectedParentJob, expectedContinuationJob }, + new List + { + expectedParentJob, + expectedContinuationJob + }, "the parent id has no correlation id but the continuation job should have the parent job id for correlation id" ); } [Fact] - public async Task Given_parent_job_is_queued_when_queueing_continuation_inside_of_correlation_context_should_use_correlation_id_from_context() + public async Task + Given_parent_job_is_queued_when_queueing_continuation_inside_of_correlation_context_should_use_correlation_id_from_context() { - var expectedParentJob = new BackgroundTestExecutor { CorrelationId = "parent-id", JobHasCompleted = true }; - var expectedContinuationJob = new BackgroundTestExecutor { CorrelationId = "continuation-id", JobHasCompleted = true }; + var expectedParentJob = new BackgroundTestExecutor + { + CorrelationId = "parent-id", + JobHasCompleted = true + }; + var expectedContinuationJob = new BackgroundTestExecutor + { + CorrelationId = "continuation-id", + JobHasCompleted = true + }; // Queue parent - _correlationManager.Correlate(expectedParentJob.CorrelationId, + _correlationManager.Correlate( + expectedParentJob.CorrelationId, () => { - expectedParentJob.JobId = _client.Enqueue( - job => job.RunAsync(TimeoutInMillis, null!) - ); - }); + expectedParentJob.JobId = _client.Enqueue(job => job.RunAsync(TimeoutInMillis, null!)); + } + ); // Act - _correlationManager.Correlate(expectedContinuationJob.CorrelationId, + _correlationManager.Correlate( + expectedContinuationJob.CorrelationId, () => { expectedContinuationJob.JobId = _client.ContinueJobWith( expectedParentJob.JobId, job => job.RunAsync(TimeoutInMillis, null!) ); - }); + } + ); await WaitUntilJobCompletedAsync(expectedContinuationJob.JobId); // Assert ExecutedJobs.Should() .BeEquivalentTo( - new List { expectedParentJob, expectedContinuationJob }, + new List + { + expectedParentJob, + expectedContinuationJob + }, "the continuation job should have the same correlation id" ); } [Fact] - public async Task Given_parent_job_is_queued_when_queueing_continuation_outside_of_correlation_context_should_use_same_correlation_id_as_parent() + public async Task + Given_parent_job_is_queued_when_queueing_continuation_outside_of_correlation_context_should_use_same_correlation_id_as_parent() { const string correlationId = "my-id"; - var expectedParentJob = new BackgroundTestExecutor { CorrelationId = correlationId, JobHasCompleted = true }; - var expectedContinuationJob = new BackgroundTestExecutor { CorrelationId = correlationId, JobHasCompleted = true }; + var expectedParentJob = new BackgroundTestExecutor + { + CorrelationId = correlationId, + JobHasCompleted = true + }; + var expectedContinuationJob = new BackgroundTestExecutor + { + CorrelationId = correlationId, + JobHasCompleted = true + }; // Queue parent - _correlationManager.Correlate(correlationId, + _correlationManager.Correlate( + correlationId, () => { - expectedParentJob.JobId = _client.Enqueue( - job => job.RunAsync(TimeoutInMillis, null!) - ); - }); + expectedParentJob.JobId = _client.Enqueue(job => job.RunAsync(TimeoutInMillis, null!)); + } + ); // Act // We queue on purpose outside of context, so we are able to test if the job inherits the correlation id from parent job. @@ -283,7 +332,11 @@ public async Task Given_parent_job_is_queued_when_queueing_continuation_outside_ // Assert ExecutedJobs.Should() .BeEquivalentTo( - new List { expectedParentJob, expectedContinuationJob }, + new List + { + expectedParentJob, + expectedContinuationJob + }, "the continuation job should have the same correlation id" ); } diff --git a/test/Hangfire.Correlate.Tests/HangfireServiceProviderTests.cs b/test/Hangfire.Correlate.Tests/HangfireServiceProviderTests.cs index 1838af7..2caa5e9 100644 --- a/test/Hangfire.Correlate.Tests/HangfireServiceProviderTests.cs +++ b/test/Hangfire.Correlate.Tests/HangfireServiceProviderTests.cs @@ -7,10 +7,13 @@ namespace Hangfire.Correlate; /// -/// Tests Hangfire integration with the overload. +/// Tests Hangfire integration with the +/// overload. /// /// -/// Parallel test execution is not supported since we use memory storage with Hangfire that is being set into a static property Storage.Current. When tests are run in parallel, the test that last set the storage will win, while the others will break. This is also true for other Hangfire dependencies, but they do not directly affect our tests atm. +/// Parallel test execution is not supported since we use memory storage with Hangfire that is being set into a static property +/// Storage.Current. When tests are run in parallel, the test that last set the storage will win, while the others will break. This is also +/// true for other Hangfire dependencies, but they do not directly affect our tests atm. /// public class HangfireServiceProviderTests : HangfireIntegrationTests { @@ -22,22 +25,26 @@ public HangfireServiceProviderTests(ITestOutputHelper testOutputHelper) : base(t IServiceCollection serviceCollection = new ServiceCollection(); serviceCollection .AddCorrelate() - .AddHangfire((s, config) => - { - config - .UseCorrelate(s) - .UseMemoryStorage(); - }); + .AddHangfire( + (s, config) => + { + config + .UseCorrelate(s) + .UseMemoryStorage(); + } + ); // Below, dependencies for test only. // Register a typed client which is used by the job to call an endpoint. // We use it to assert the request header contains the correlation id. serviceCollection - .AddHttpClient(client => - { - client.BaseAddress = new Uri("http://0.0.0.0"); - }) + .AddHttpClient( + client => + { + client.BaseAddress = new Uri("http://0.0.0.0"); + } + ) .ConfigurePrimaryHttpMessageHandler(() => MockHttp) .CorrelateRequests();