Skip to content

test(hangfire): use thread-safe Hangfire.InMemory storage in tests - #5905

Merged
jamescrosswell merged 1 commit into
mainfrom
fix/hangfire-tests-inmemory-storage
Nov 11, 2026
Merged

jamescrosswell merged 1 commit into
mainfrom
fix/hangfire-tests-inmemory-storage

Conversation

@jamescrosswell

Copy link
Copy Markdown
Collaborator

Summary

Fixes the flaky HangfireTests failure InvalidOperationException: Collection was modified thrown from Hangfire.MemoryStorage's MemoryStorageMonitoringApi.JobDetails. HangfireFixture.WaitForJob polls that API while the BackgroundJobServer worker is updating the same job, and MemoryStorage isn't safe for that.

The test project now uses the official, thread-safe Hangfire.InMemory storage (UseInMemoryStorage()) instead of the community Hangfire.MemoryStorage package.

Hangfire.InMemory returns JobDetailsDto.History newest-first (like the SQL Server storage), whereas MemoryStorage returned it oldest-first, so the fixture now reads the current state from History[0].

Test-only change; Sentry.Hangfire itself is untouched.

Closes #5904

#skip-changelog

🤖 Generated with Claude Code

Hangfire.MemoryStorage's monitoring API enumerates job state lists while the
server is writing them, which made WaitForJob throw "Collection was modified".
Hangfire.InMemory returns history newest-first, so read History[0].

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.87%. Comparing base (d30060f) to head (b85ff3a).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5905      +/-   ##
==========================================
+ Coverage   76.86%   76.87%   +0.01%     
==========================================
  Files         533      533              
  Lines       19847    19847              
  Branches     3920     3920              
==========================================
+ Hits        15256    15258       +2     
+ Misses       3710     3709       -1     
+ Partials      881      880       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jamescrosswell
jamescrosswell marked this pull request as ready for review November 10, 2026 22:44
@github-actions github-actions Bot added the risk: low PR risk score: low label Nov 10, 2026

@ric-oliv ric-oliv left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@jamescrosswell
jamescrosswell merged commit 01d63ef into main Nov 11, 2026
44 checks passed
@jamescrosswell
jamescrosswell deleted the fix/hangfire-tests-inmemory-storage branch November 11, 2026 02:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: low PR risk score: low

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flaky test: Hangfire tests race Hangfire.MemoryStorage while polling job state

2 participants