-
Notifications
You must be signed in to change notification settings - Fork 4
Be explicit about SaveChanges #2035
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
Closed
Closed
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
0cfc3b4
EnableRetryOnFailure does not handle explicit transactions properly
deee1dc
Adjusted retry attempts down to 5 with exponential backoff
6897cb2
Added tests to verify all the retry behaviour for database transactions
66d22df
Complete re-factor to EnableRetryOnFailure
7e9e570
Minor fixes
e7db6a8
Limit retry count when running tests
3d622f4
Revert "Limit retry count when running tests"
7866d85
Remove PostgresTestcontainerFixture and rely on same postgres server …
4194b1f
Try adjusting pool size to make tests more reliable
764d394
Fix remaning old style transactions
4c7ae00
Wait longer for attachment upload to make tests less flaky
3d685f2
Pass in cancellationToken
4d61e00
Do not upload to blob inside retry
84c3350
Fix OpsGenie
f8a55a0
Deferring saving changes to avoid undue database load, and also re-fa…
32888b2
Add tests
edd2a3a
Added tests for uniqueness violation
fc3f35b
Fixed upload case
ae715fc
Re-wrote to be explicit about SaveChanges wherever needed
92241da
Merge branch 'main' into feat/deferred-save-changes
Ceredron 64c20bb
Fix build
6187bd6
Try to make tests less flaky
af9b7ab
Fix test
90b0569
Fixes from comments
fba8a1a
Loosen up a bit on the polling
0092f36
Merge branch 'main' into feat/deferred-save-changes
c01357f
Merge from main
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
Test/Altinn.Correspondence.Tests/Fixtures/UniqueViolationOnDeferredSaveDbContext.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| using Altinn.Correspondence.Persistence; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Npgsql; | ||
|
|
||
| namespace Altinn.Correspondence.Tests.Fixtures; | ||
|
|
||
| /// <summary> | ||
| /// Simulates a concurrent idempotency insert losing the race on the batched flush inside | ||
| /// <see cref="Altinn.Correspondence.Application.Helpers.DatabaseTransactionHelper.ExecuteAsync"/>. | ||
| /// </summary> | ||
| public sealed class UniqueViolationOnDeferredSaveDbContext( | ||
| DbContextOptions<ApplicationDbContext> options, | ||
| int uniqueViolationOnSaveAttempt = 1) | ||
| : TestApplicationDbContext(options) | ||
| { | ||
| private int _saveAttempts; | ||
|
|
||
| public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default) | ||
| { | ||
| _saveAttempts++; | ||
| if (_saveAttempts == uniqueViolationOnSaveAttempt) | ||
| { | ||
| throw new DbUpdateException( | ||
| "duplicate key", | ||
| new PostgresException( | ||
| "duplicate key value violates unique constraint", | ||
| "ERROR", | ||
| "ERROR", | ||
| "23505")); | ||
| } | ||
|
|
||
| return await base.SaveChangesAsync(cancellationToken); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: Altinn/altinn-correspondence
Length of output: 50385
🏁 Script executed:
Repository: Altinn/altinn-correspondence
Length of output: 18915
🏁 Script executed:
Repository: Altinn/altinn-correspondence
Length of output: 50384
🏁 Script executed:
Repository: Altinn/altinn-correspondence
Length of output: 18822
Set a test-only Hangfire worker count.
CustomWebApplicationFactoryno longer setsWorkerCount, so the test server now uses Hangfire’s default. This test assembly already uses explicit worker counts where concurrency/ordering is relevant, so add an explicit test-only value for this configuration too.🤖 Prompt for AI Agents