-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
229 additions
and
7 deletions.
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,222 @@ | ||
using Moq; | ||
using ASureBus.Core; | ||
using ASureBus.Core.Caching; | ||
using ASureBus.Core.Messaging; | ||
using ASureBus.Core.Sagas; | ||
using ASureBus.Core.TypesHandling; | ||
using ASureBus.Core.TypesHandling.Entities; | ||
using ASureBus.Services.ServiceBus; | ||
using Azure.Messaging.ServiceBus; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace ASureBus.Tests.ASureBus.Core; | ||
|
||
[TestFixture] | ||
public class AsbWorkerTests | ||
{ | ||
[Test] | ||
public async Task StartAsync_StartsAllProcessors_ForHandlers() | ||
{ | ||
// Arrange | ||
var mockProcessor = new Mock<ServiceBusProcessor>(); | ||
var mockAzureServiceBusService = new Mock<IAzureServiceBusService>(); | ||
var mockTypesLoader = new Mock<ITypesLoader>(); | ||
|
||
var anHandler = new HandlerType() | ||
{ | ||
MessageType = new MessageType() | ||
{ | ||
IsCommand = true, | ||
Type = typeof(object) | ||
}, | ||
Type = typeof(object) | ||
}; | ||
|
||
var handlerSet = new[] | ||
{ | ||
anHandler | ||
}.ToHashSet(); | ||
|
||
mockTypesLoader.Setup(t => t.Handlers).Returns(handlerSet); | ||
mockTypesLoader.Setup(t => t.Sagas).Returns(Array.Empty<SagaType>().ToHashSet()); | ||
|
||
mockAzureServiceBusService | ||
.Setup(a => a.GetProcessor(handlerSet.FirstOrDefault()!, It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(mockProcessor.Object); | ||
|
||
var worker = new AsbWorker( | ||
Mock.Of<IHostApplicationLifetime>(), | ||
Mock.Of<IServiceProvider>(), | ||
mockAzureServiceBusService.Object, | ||
Mock.Of<IMessageEmitter>(), | ||
mockTypesLoader.Object, | ||
Mock.Of<IAsbCache>(), | ||
Mock.Of<ISagaBehaviour>()); | ||
|
||
// Act | ||
await worker.StartAsync(CancellationToken.None); | ||
|
||
// Assert | ||
mockProcessor.Verify(p => p.StartProcessingAsync(It.IsAny<CancellationToken>()), | ||
Times.Once); | ||
} | ||
|
||
[Test] | ||
public async Task StartAsync_StartsAllProcessors_ForSagas() | ||
{ | ||
// Arrange | ||
var mockProcessor = new Mock<ServiceBusProcessor>(); | ||
var mockAzureServiceBusService = new Mock<IAzureServiceBusService>(); | ||
var mockTypesLoader = new Mock<ITypesLoader>(); | ||
|
||
var sagaHandlerType = new SagaHandlerType() | ||
{ | ||
IsInitMessageHandler = true, | ||
MessageType = new MessageType() | ||
{ | ||
IsCommand = true, | ||
Type = typeof(object) | ||
} | ||
}; | ||
|
||
var aSaga = new SagaType() | ||
{ | ||
Type = typeof(object), | ||
Listeners = new[] | ||
{ | ||
sagaHandlerType | ||
}.ToHashSet() | ||
}; | ||
|
||
var sagaSet = new[] | ||
{ | ||
aSaga | ||
}.ToHashSet(); | ||
|
||
mockTypesLoader.Setup(t => t.Handlers).Returns(Array.Empty<HandlerType>().ToHashSet()); | ||
mockTypesLoader.Setup(t => t.Sagas).Returns(sagaSet); | ||
|
||
mockAzureServiceBusService | ||
.Setup(a => a.GetProcessor(sagaHandlerType, It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(mockProcessor.Object); | ||
|
||
var worker = new AsbWorker( | ||
Mock.Of<IHostApplicationLifetime>(), | ||
Mock.Of<IServiceProvider>(), | ||
mockAzureServiceBusService.Object, | ||
Mock.Of<IMessageEmitter>(), | ||
mockTypesLoader.Object, | ||
Mock.Of<IAsbCache>(), | ||
Mock.Of<ISagaBehaviour>()); | ||
|
||
// Act | ||
await worker.StartAsync(CancellationToken.None); | ||
|
||
// Assert | ||
mockProcessor.Verify(p => p.StartProcessingAsync(It.IsAny<CancellationToken>()), | ||
Times.Once); | ||
} | ||
|
||
[Test] | ||
public async Task StopAsync_StopsAndDisposesAllProcessors_ForHandlers() | ||
{ | ||
// Arrange | ||
var mockProcessor = new Mock<ServiceBusProcessor>(); | ||
var mockAzureServiceBusService = new Mock<IAzureServiceBusService>(); | ||
var mockTypesLoader = new Mock<ITypesLoader>(); | ||
|
||
var anHandler = new HandlerType() | ||
{ | ||
MessageType = new MessageType() | ||
{ | ||
IsCommand = true, | ||
Type = typeof(object) | ||
}, | ||
Type = typeof(object) | ||
}; | ||
|
||
var handlerSet = new[] | ||
{ | ||
anHandler | ||
}.ToHashSet(); | ||
|
||
mockTypesLoader.Setup(t => t.Handlers).Returns(handlerSet); | ||
mockTypesLoader.Setup(t => t.Sagas).Returns(Array.Empty<SagaType>().ToHashSet()); | ||
|
||
mockAzureServiceBusService | ||
.Setup(a => a.GetProcessor(handlerSet.FirstOrDefault()!, It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(mockProcessor.Object); | ||
|
||
var worker = new AsbWorker( | ||
Mock.Of<IHostApplicationLifetime>(), | ||
Mock.Of<IServiceProvider>(), | ||
mockAzureServiceBusService.Object, | ||
Mock.Of<IMessageEmitter>(), | ||
mockTypesLoader.Object, | ||
Mock.Of<IAsbCache>(), | ||
Mock.Of<ISagaBehaviour>()); | ||
|
||
// Act | ||
await worker.StartAsync(CancellationToken.None); | ||
await worker.StopAsync(CancellationToken.None); | ||
|
||
// Assert | ||
mockProcessor.Verify(p => p.StopProcessingAsync(It.IsAny<CancellationToken>()), Times.Once); | ||
} | ||
|
||
[Test] | ||
public async Task StopAsync_StopsAndDisposesAllProcessors_ForSagas() | ||
{ | ||
// Arrange | ||
var mockProcessor = new Mock<ServiceBusProcessor>(); | ||
var mockAzureServiceBusService = new Mock<IAzureServiceBusService>(); | ||
var mockTypesLoader = new Mock<ITypesLoader>(); | ||
|
||
var sagaHandlerType = new SagaHandlerType() | ||
{ | ||
IsInitMessageHandler = true, | ||
MessageType = new MessageType() | ||
{ | ||
IsCommand = true, | ||
Type = typeof(object) | ||
} | ||
}; | ||
|
||
var aSaga = new SagaType() | ||
{ | ||
Type = typeof(object), | ||
Listeners = new[] | ||
{ | ||
sagaHandlerType | ||
}.ToHashSet() | ||
}; | ||
|
||
var sagaSet = new[] | ||
{ | ||
aSaga | ||
}.ToHashSet(); | ||
|
||
mockTypesLoader.Setup(t => t.Handlers).Returns(Array.Empty<HandlerType>().ToHashSet()); | ||
mockTypesLoader.Setup(t => t.Sagas).Returns(sagaSet); | ||
|
||
mockAzureServiceBusService | ||
.Setup(a => a.GetProcessor(sagaHandlerType, It.IsAny<CancellationToken>())) | ||
.ReturnsAsync(mockProcessor.Object); | ||
|
||
var worker = new AsbWorker( | ||
Mock.Of<IHostApplicationLifetime>(), | ||
Mock.Of<IServiceProvider>(), | ||
mockAzureServiceBusService.Object, | ||
Mock.Of<IMessageEmitter>(), | ||
mockTypesLoader.Object, | ||
Mock.Of<IAsbCache>(), | ||
Mock.Of<ISagaBehaviour>()); | ||
|
||
// Act | ||
await worker.StartAsync(CancellationToken.None); | ||
await worker.StopAsync(CancellationToken.None); | ||
|
||
// Assert | ||
mockProcessor.Verify(p => p.StopProcessingAsync(It.IsAny<CancellationToken>()), Times.Once); | ||
} | ||
} |
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