Skip to content

[Hangfire] Fix flaky test #2835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -3,7 +3,6 @@

using System.Diagnostics;
using Hangfire;
using Hangfire.Storage.Monitoring;
using OpenTelemetry.Trace;
using Xunit;

Expand Down Expand Up @@ -173,13 +172,29 @@ public async Task Should_Respect_Filter_Option(string filter, bool shouldRecord)

private async Task WaitJobProcessedAsync(string jobId, int timeToWaitInSeconds)
{
var timeout = DateTime.Now.AddSeconds(timeToWaitInSeconds);
var timeout = TimeSpan.FromSeconds(timeToWaitInSeconds);
using var cts = new CancellationTokenSource(timeout);

string[] states = ["Enqueued", "Processing"];
JobDetailsDto jobDetails;
while (((jobDetails = this.hangfireFixture.MonitoringApi.JobDetails(jobId)) == null || jobDetails.History.All(h => states.Contains(h.StateName)))
&& DateTime.Now < timeout)

while (!Completed() && !cts.IsCancellationRequested)
{
await Task.Delay(500);
}

bool Completed()
{
var jobDetails = this.hangfireFixture.MonitoringApi.JobDetails(jobId);

if (jobDetails == null)
{
return false;
}

// Copy the history to an array to avoid exception if the collection is modified while iterating
var history = jobDetails.History.ToArray();

return !history.All(h => states.Contains(h.StateName));
}
}
}
Loading