Skip to content

Commit

Permalink
style: apply (new) code style (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
skwasjer authored Oct 6, 2024
1 parent 52479cc commit 7a1c224
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 58 deletions.
2 changes: 1 addition & 1 deletion src/Hangfire.Correlate/Properties/InternalsVisibleTo.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Hangfire.Correlate.Tests")]
[assembly: InternalsVisibleTo("Hangfire.Correlate.Tests")]
7 changes: 6 additions & 1 deletion test/Hangfire.Correlate.Tests/BackgroundTestExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ internal BackgroundTestExecutor()
{
}

public BackgroundTestExecutor(TestService testService, ICorrelationContextAccessor correlationContextAccessor, ICollection<object> jobState, ITestOutputHelper testOutputHelper)
public BackgroundTestExecutor(
TestService testService,
ICorrelationContextAccessor correlationContextAccessor,
ICollection<object> jobState,
ITestOutputHelper testOutputHelper
)
{
_testService = testService ?? throw new ArgumentNullException(nameof(testService));
_correlationContextAccessor = correlationContextAccessor ?? throw new ArgumentNullException(nameof(correlationContextAccessor));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
11 changes: 4 additions & 7 deletions test/Hangfire.Correlate.Tests/GlobalTestContext.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using Hangfire.Common;
using Xunit;

namespace Hangfire.Correlate;
namespace Hangfire.Correlate;

/// <summary>
/// Marker to disable parallel tests because when registering Hangfire filters, it adds it to a static list.
Expand All @@ -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);
}
Expand Down
125 changes: 89 additions & 36 deletions test/Hangfire.Correlate.Tests/HangfireIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ private async Task WaitUntilJobCompletedAsync(string jobId, int maxWaitInMillise
var sw = Stopwatch.StartNew();
StateHistoryDto[] jobHistory = Array.Empty<StateHistoryDto>();
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);
Expand Down Expand Up @@ -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<BackgroundTestExecutor>(job => job.RunAsync(TimeoutInMillis, null!));
});
}
);

while (true)
{
Expand All @@ -123,6 +125,7 @@ await _correlationManager.CorrelateAsync(correlationId,
break;
}
}

await WaitUntilJobCompletedAsync(jobId.Should().NotBeNull().And.Subject);

// Assert
Expand All @@ -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<BackgroundTestExecutor>(job => job.RunAsync(TimeoutInMillis, null!));
});
}
);

await WaitUntilJobCompletedAsync(expectedJob.JobId);

// Assert
ExecutedJobs.Should()
.BeEquivalentTo(
new List<object> { expectedJob },
new List<object>
{
expectedJob
},
"a correlation context exists, so the correlation id should be used when performing the job"
);
}
Expand Down Expand Up @@ -188,21 +200,30 @@ public async Task Given_job_is_queued_outside_correlationContext_should_use_jobI
// Assert
ExecutedJobs.Should()
.BeEquivalentTo(
new List<object> { expectedJob },
new List<object>
{
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<BackgroundTestExecutor>(
job => job.RunAsync(TimeoutInMillis, null!)
);
expectedParentJob.JobId = expectedParentJob.CorrelationId = expectedContinuationJob.CorrelationId =
_client.Enqueue<BackgroundTestExecutor>(job => job.RunAsync(TimeoutInMillis, null!));

// Act
expectedContinuationJob.JobId = _client.ContinueJobWith<BackgroundTestExecutor>(
Expand All @@ -215,61 +236,89 @@ public async Task Given_parent_job_is_queued_outside_correlation_context_when_qu
// Assert
ExecutedJobs.Should()
.BeEquivalentTo(
new List<object> { expectedParentJob, expectedContinuationJob },
new List<object>
{
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<BackgroundTestExecutor>(
job => job.RunAsync(TimeoutInMillis, null!)
);
});
expectedParentJob.JobId = _client.Enqueue<BackgroundTestExecutor>(job => job.RunAsync(TimeoutInMillis, null!));
}
);

// Act
_correlationManager.Correlate(expectedContinuationJob.CorrelationId,
_correlationManager.Correlate(
expectedContinuationJob.CorrelationId,
() =>
{
expectedContinuationJob.JobId = _client.ContinueJobWith<BackgroundTestExecutor>(
expectedParentJob.JobId,
job => job.RunAsync(TimeoutInMillis, null!)
);
});
}
);

await WaitUntilJobCompletedAsync(expectedContinuationJob.JobId);

// Assert
ExecutedJobs.Should()
.BeEquivalentTo(
new List<object> { expectedParentJob, expectedContinuationJob },
new List<object>
{
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<BackgroundTestExecutor>(
job => job.RunAsync(TimeoutInMillis, null!)
);
});
expectedParentJob.JobId = _client.Enqueue<BackgroundTestExecutor>(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.
Expand All @@ -283,7 +332,11 @@ public async Task Given_parent_job_is_queued_when_queueing_continuation_outside_
// Assert
ExecutedJobs.Should()
.BeEquivalentTo(
new List<object> { expectedParentJob, expectedContinuationJob },
new List<object>
{
expectedParentJob,
expectedContinuationJob
},
"the continuation job should have the same correlation id"
);
}
Expand Down
31 changes: 19 additions & 12 deletions test/Hangfire.Correlate.Tests/HangfireServiceProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
namespace Hangfire.Correlate;

/// <summary>
/// Tests Hangfire integration with the <see cref="GlobalConfigurationExtensions.UseCorrelate(IGlobalConfiguration,IServiceProvider)" /> overload.
/// Tests Hangfire integration with the <see cref="GlobalConfigurationExtensions.UseCorrelate(IGlobalConfiguration,IServiceProvider)" />
/// overload.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
public class HangfireServiceProviderTests : HangfireIntegrationTests
{
Expand All @@ -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<TestService>(client =>
{
client.BaseAddress = new Uri("http://0.0.0.0");
})
.AddHttpClient<TestService>(
client =>
{
client.BaseAddress = new Uri("http://0.0.0.0");
}
)
.ConfigurePrimaryHttpMessageHandler(() => MockHttp)
.CorrelateRequests();

Expand Down

0 comments on commit 7a1c224

Please sign in to comment.