Repository navigation
Derive failed error import id from the message, not a random Guid #5652
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
Merged
Merged
Changes from all commits
Commits
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
50 changes: 50 additions & 0 deletions
50
src/ServiceControl.Persistence.Tests.RavenDB/Operations/FailedErrorImportDedupeTests.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,50 @@ | ||
| namespace ServiceControl.Persistence.Tests.RavenDB.Operations | ||
| { | ||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
| using NServiceBus; | ||
| using NUnit.Framework; | ||
| using Raven.Client.Documents; | ||
| using ServiceControl.Operations; | ||
|
|
||
| [TestFixture] | ||
| class FailedErrorImportDedupeTests : RavenPersistenceTestBase | ||
| { | ||
| [Test] | ||
| public async Task Repeated_failure_of_the_same_message_stores_one_document() | ||
| { | ||
| var headers = new Dictionary<string, string> | ||
| { | ||
| { Headers.MessageId, "message-1" }, | ||
| { Headers.ProcessingEndpoint, "Sales" } | ||
| }; | ||
|
|
||
| await StoreFailure(headers, "the first failure"); | ||
| await StoreFailure(headers, "the second failure"); | ||
|
|
||
| DocumentStore.WaitForIndexing(); | ||
|
|
||
| using var session = DocumentStore.OpenAsyncSession(); | ||
| var documents = await session.Query<FailedErrorImport>().ToListAsync(); | ||
|
|
||
| using (Assert.EnterMultipleScope()) | ||
| { | ||
| Assert.That(documents, Has.Count.EqualTo(1)); | ||
| Assert.That(documents[0].ExceptionInfo, Is.EqualTo("the second failure")); | ||
| } | ||
| } | ||
|
|
||
| Task StoreFailure(IReadOnlyDictionary<string, string> headers, string exceptionInfo) => | ||
| ErrorStore.StoreFailedErrorImport(new FailedErrorImport | ||
| { | ||
| Id = FailedErrorImport.MakeDocumentId(FailedErrorImport.DeriveKey(headers, "native-1")), | ||
| Message = new FailedTransportMessage | ||
| { | ||
| Id = "native-1", | ||
| Headers = new Dictionary<string, string>(headers), | ||
| Body = [] | ||
| }, | ||
| ExceptionInfo = exceptionInfo | ||
| }); | ||
| } | ||
| } |
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
75 changes: 75 additions & 0 deletions
75
src/ServiceControl.UnitTests/Operations/When_deriving_a_failed_error_import_key.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,75 @@ | ||
| namespace ServiceControl.UnitTests.Operations; | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using NServiceBus; | ||
| using NUnit.Framework; | ||
| using ServiceControl.Operations; | ||
| using ServiceControl.Persistence.Infrastructure; | ||
|
|
||
| [TestFixture] | ||
| public class When_deriving_a_failed_error_import_key | ||
| { | ||
| [Test] | ||
| public void Uses_the_unique_message_id_when_headers_are_well_formed() | ||
| { | ||
| var headers = new Dictionary<string, string> | ||
| { | ||
| { Headers.MessageId, "message-1" }, | ||
| { Headers.ProcessingEndpoint, "Sales" } | ||
| }; | ||
|
|
||
| var key = FailedErrorImport.DeriveKey(headers, "native-1"); | ||
|
|
||
| Assert.That(key, Is.EqualTo(DeterministicGuid.MakeId("message-1", "Sales"))); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Prefers_an_existing_retry_unique_message_id() | ||
| { | ||
| var uniqueMessageId = Guid.NewGuid(); | ||
| var headers = new Dictionary<string, string> | ||
| { | ||
| { "ServiceControl.Retry.UniqueMessageId", uniqueMessageId.ToString() } | ||
| }; | ||
|
|
||
| var key = FailedErrorImport.DeriveKey(headers, "native-1"); | ||
|
|
||
| Assert.That(key, Is.EqualTo(uniqueMessageId)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Falls_back_to_the_native_id_when_no_processing_endpoint_can_be_derived() | ||
| { | ||
| var headers = new Dictionary<string, string> | ||
| { | ||
| { Headers.MessageId, "message-1" } | ||
| }; | ||
|
|
||
| var key = FailedErrorImport.DeriveKey(headers, "native-1"); | ||
|
|
||
| Assert.That(key, Is.EqualTo(DeterministicGuid.MakeId("native-1"))); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Falls_back_when_the_retry_unique_message_id_is_not_a_guid() | ||
| { | ||
| var headers = new Dictionary<string, string> | ||
| { | ||
| { "ServiceControl.Retry.UniqueMessageId", "not-a-guid" } | ||
| }; | ||
|
|
||
| var key = FailedErrorImport.DeriveKey(headers, "native-1"); | ||
|
|
||
| Assert.That(key, Is.EqualTo(DeterministicGuid.MakeId("native-1"))); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Is_stable_across_repeated_failures_of_the_same_malformed_message() | ||
| { | ||
| var first = FailedErrorImport.DeriveKey(new Dictionary<string, string>(), "native-1"); | ||
| var second = FailedErrorImport.DeriveKey(new Dictionary<string, string>(), "native-1"); | ||
|
|
||
| Assert.That(second, Is.EqualTo(first)); | ||
| } | ||
| } |
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.
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.
Would it be better to use DeterministicGuid.MakeId on it when not a valid Guid?
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.
@rbev I am unsure what you mean?, The
UniqueId(), does useDeterministicGuid.MakeIdinternally.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.
Given the use of
TryParse()I assumed that the.UniqueId()could return strings in formats other than a Guid. In the case that it's already a unique id but not a Guid the code could have created one from that value before falling back to generating one off transport's message id.If that method always returns a Guid string then my comment is meaningless, however looking at the code it seems that it returns a raw header value if it exists.
Uh oh!
There was an error while loading. Please reload this page.
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.
that is correct and hence the reason for the fallback, are you saying my fallback is not right?