-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from AMalininHere/add-handler-invoker
[Breaking change] introduce `HandlerInvoker` to call handlers
- Loading branch information
Showing
10 changed files
with
101 additions
and
93 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Camunda.Worker.Execution | ||
{ | ||
public class HandlerInvoker | ||
{ | ||
private readonly IExternalTaskHandler _handler; | ||
private readonly IExternalTaskContext _context; | ||
|
||
public HandlerInvoker(IExternalTaskHandler handler, IExternalTaskContext context) | ||
{ | ||
_handler = Guard.NotNull(handler, nameof(handler)); | ||
_context = Guard.NotNull(context, nameof(context)); | ||
} | ||
|
||
public async Task InvokeAsync() | ||
{ | ||
IExecutionResult executionResult; | ||
try | ||
{ | ||
executionResult = await _handler.HandleAsync(_context.Task, default); | ||
} | ||
catch (Exception e) | ||
{ | ||
executionResult = new FailureResult(e); | ||
} | ||
|
||
await executionResult.ExecuteResultAsync(_context); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,9 +1,10 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Camunda.Worker | ||
{ | ||
public interface IExternalTaskHandler | ||
{ | ||
Task HandleAsync(IExternalTaskContext context); | ||
Task<IExecutionResult> HandleAsync(ExternalTask externalTask, CancellationToken cancellationToken); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace Camunda.Worker.Execution | ||
{ | ||
public class HandlerInvokerTest | ||
{ | ||
private readonly Mock<IExternalTaskHandler> _handlerMock = new(); | ||
private readonly Mock<IExternalTaskContext> _contextMock = new(); | ||
private readonly HandlerInvoker _handlerInvoker; | ||
|
||
public HandlerInvokerTest() | ||
{ | ||
_handlerInvoker = new HandlerInvoker(_handlerMock.Object, _contextMock.Object); | ||
} | ||
|
||
[Fact] | ||
public async Task TestExecuteReturnedFromHandlerResult() | ||
{ | ||
// Arrange | ||
var resultMock = new Mock<IExecutionResult>(); | ||
|
||
_handlerMock.Setup(handler => handler.HandleAsync(It.IsAny<ExternalTask>(), default)) | ||
.ReturnsAsync(resultMock.Object); | ||
|
||
// Act | ||
await _handlerInvoker.InvokeAsync(); | ||
|
||
// Assert | ||
resultMock.Verify(result => result.ExecuteResultAsync(It.IsAny<IExternalTaskContext>()), Times.Once()); | ||
} | ||
|
||
[Fact] | ||
public async Task TestReportFailureIfHandlerFails() | ||
{ | ||
// Arrange | ||
_handlerMock.Setup(handler => handler.HandleAsync(It.IsAny<ExternalTask>(), default)) | ||
.ThrowsAsync(new Exception()); | ||
|
||
// Act | ||
await _handlerInvoker.InvokeAsync(); | ||
|
||
// Assert | ||
_contextMock.Verify( | ||
context => context.ReportFailureAsync(It.IsAny<string>(), It.IsAny<string>(), null, null), | ||
Times.Once() | ||
); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.