-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[PM-17562] Refactor existing RabbitMq implementation #5357
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1314586
[PM-17562] Refactor existing RabbitMq implementation
brant-livefront 88b39ba
Merge branch 'main' into brant/refactor-event-handlers
brant-livefront f610bee
Fixed issues noted in PR review
brant-livefront 159cede
Merge branch 'main' into brant/refactor-event-handlers
brant-livefront 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 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,8 @@ | ||
๏ปฟusing Bit.Core.Models.Data; | ||
|
||
namespace Bit.Core.Services; | ||
|
||
public interface IEventMessageHandler | ||
{ | ||
Task HandleEventAsync(EventMessage eventMessage); | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Core/AdminConsole/Services/Implementations/EventRepositoryHandler.cs
This file contains 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,14 @@ | ||
๏ปฟusing Bit.Core.Models.Data; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Bit.Core.Services; | ||
|
||
public class EventRepositoryHandler( | ||
[FromKeyedServices("persistent")] IEventWriteService eventWriteService) | ||
: IEventMessageHandler | ||
{ | ||
public Task HandleEventAsync(EventMessage eventMessage) | ||
{ | ||
return eventWriteService.CreateAsync(eventMessage); | ||
} | ||
} |
15 changes: 4 additions & 11 deletions
15
...ntations/RabbitMqEventHttpPostListener.cs โ ...s/Implementations/HttpPostEventHandler.cs
This file contains 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
29 changes: 0 additions & 29 deletions
29
src/Core/AdminConsole/Services/Implementations/RabbitMqEventRepositoryListener.cs
This file was deleted.
Oops, something went wrong.
This file contains 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,13 @@ | ||
๏ปฟusing Microsoft.Extensions.Hosting; | ||
|
||
namespace Bit.Core.Services; | ||
|
||
public abstract class EventLoggingListenerService : BackgroundService | ||
{ | ||
protected readonly IEventMessageHandler _handler; | ||
|
||
protected EventLoggingListenerService(IEventMessageHandler handler) | ||
{ | ||
_handler = handler ?? throw new ArgumentNullException(nameof(handler)); | ||
} | ||
} |
This file contains 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 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 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 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
24 changes: 24 additions & 0 deletions
24
test/Core.Test/AdminConsole/Services/EventRepositoryHandlerTests.cs
This file contains 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,24 @@ | ||
๏ปฟusing Bit.Core.Models.Data; | ||
using Bit.Core.Services; | ||
using Bit.Test.Common.AutoFixture; | ||
using Bit.Test.Common.AutoFixture.Attributes; | ||
using Bit.Test.Common.Helpers; | ||
using NSubstitute; | ||
using Xunit; | ||
|
||
namespace Bit.Core.Test.Services; | ||
|
||
[SutProviderCustomize] | ||
public class EventRepositoryHandlerTests | ||
{ | ||
[Theory, BitAutoData] | ||
public async Task HandleEventAsync_WritesEventToIEventWriteService( | ||
EventMessage eventMessage, | ||
SutProvider<EventRepositoryHandler> sutProvider) | ||
{ | ||
await sutProvider.Sut.HandleEventAsync(eventMessage); | ||
await sutProvider.GetDependency<IEventWriteService>().Received(1).CreateAsync( | ||
Arg.Is(AssertHelper.AssertPropertyEqual<IEvent>(eventMessage)) | ||
); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
test/Core.Test/AdminConsole/Services/HttpPostEventHandlerTests.cs
This file contains 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,66 @@ | ||
๏ปฟusing System.Net; | ||
using System.Net.Http.Json; | ||
using Bit.Core.Models.Data; | ||
using Bit.Core.Services; | ||
using Bit.Test.Common.AutoFixture; | ||
using Bit.Test.Common.AutoFixture.Attributes; | ||
using Bit.Test.Common.Helpers; | ||
using Bit.Test.Common.MockedHttpClient; | ||
using NSubstitute; | ||
using Xunit; | ||
using GlobalSettings = Bit.Core.Settings.GlobalSettings; | ||
|
||
namespace Bit.Core.Test.Services; | ||
|
||
[SutProviderCustomize] | ||
public class HttpPostEventHandlerTests | ||
{ | ||
private readonly MockedHttpMessageHandler _handler; | ||
private HttpClient _httpClient; | ||
|
||
private const string _httpPostUrl = "http://localhost/test/event"; | ||
|
||
public HttpPostEventHandlerTests() | ||
{ | ||
_handler = new MockedHttpMessageHandler(); | ||
_handler.Fallback | ||
.WithStatusCode(HttpStatusCode.OK) | ||
.WithContent(new StringContent("<html><head><title>test</title></head><body>test</body></html>")); | ||
_httpClient = _handler.ToHttpClient(); | ||
} | ||
|
||
public SutProvider<HttpPostEventHandler> GetSutProvider() | ||
{ | ||
var clientFactory = Substitute.For<IHttpClientFactory>(); | ||
clientFactory.CreateClient(HttpPostEventHandler.HttpClientName).Returns(_httpClient); | ||
|
||
var globalSettings = new GlobalSettings(); | ||
globalSettings.EventLogging.RabbitMq.HttpPostUrl = _httpPostUrl; | ||
|
||
return new SutProvider<HttpPostEventHandler>() | ||
.SetDependency(globalSettings) | ||
.SetDependency(clientFactory) | ||
.Create(); | ||
} | ||
|
||
[Theory, BitAutoData] | ||
public async Task HandleEventAsync_PostsEventsToUrl(EventMessage eventMessage) | ||
{ | ||
var sutProvider = GetSutProvider(); | ||
var content = JsonContent.Create(eventMessage); | ||
|
||
await sutProvider.Sut.HandleEventAsync(eventMessage); | ||
sutProvider.GetDependency<IHttpClientFactory>().Received(1).CreateClient( | ||
Arg.Is(AssertHelper.AssertPropertyEqual<string>(HttpPostEventHandler.HttpClientName)) | ||
); | ||
|
||
Assert.Single(_handler.CapturedRequests); | ||
var request = _handler.CapturedRequests[0]; | ||
Assert.NotNull(request); | ||
var returned = await request.Content.ReadFromJsonAsync<EventMessage>(); | ||
|
||
Assert.Equal(HttpMethod.Post, request.Method); | ||
Assert.Equal(_httpPostUrl, request.RequestUri.ToString()); | ||
AssertHelper.AssertPropertyEqual(eventMessage, returned, new[] { "IdempotencyId" }); | ||
} | ||
} |
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.
โน๏ธ We're not totally sure that we're gonna keep maintaining this interface as we're almost always just asking for the implementation, but this is fine.