-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves #1140
- Loading branch information
Showing
6 changed files
with
135 additions
and
3 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
src/Microsoft.Azure.WebJobs.Host/Executors/ITriggeredFunctionExecutorWithHook.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,26 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Azure.WebJobs.Host.Executors | ||
{ | ||
/// <summary> | ||
/// Interface defining the contract for executing a triggered function. | ||
/// Allows a hook around the underlying execution. | ||
/// This should only be used by extensions that need very specific control over the invocation. | ||
/// </summary> | ||
public interface ITriggeredFunctionExecutorWithHook | ||
{ | ||
/// <summary> | ||
/// Try to invoke the triggered function using the values specified. | ||
/// </summary> | ||
/// <param name="input">The trigger invocation details.</param> | ||
/// <param name="cancellationToken">The cancellation token</param> | ||
/// <param name="hook">a hook that wraps the underlying invocation</param> | ||
/// <returns>A <see cref="FunctionResult"/> describing the results of the invocation.</returns> | ||
Task<FunctionResult> TryExecuteAsync(TriggeredFunctionData input, CancellationToken cancellationToken, Func<Func<Task>, Task> hook); | ||
} | ||
} |
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
72 changes: 72 additions & 0 deletions
72
test/Microsoft.Azure.WebJobs.Host.UnitTests/Executors/TriggeredFunctionExecutorTests.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,72 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Microsoft.Azure.WebJobs.Host.Executors; | ||
using Xunit; | ||
using Microsoft.Azure.WebJobs.Host.Protocols; | ||
using Microsoft.Azure.WebJobs.Host.Triggers; | ||
using System.Threading.Tasks; | ||
using System.Threading; | ||
using System.Text; | ||
using Moq; | ||
|
||
namespace Microsoft.Azure.WebJobs.Host.UnitTests.Executors | ||
{ | ||
public class TriggeredFunctionExecutorTests | ||
{ | ||
// Test ITriggeredFunctionExecutorWithHook | ||
[Fact] | ||
public async Task TestHook() | ||
{ | ||
StringBuilder sb = new StringBuilder(); | ||
|
||
var descr = new FunctionDescriptor(); | ||
|
||
// IFunctionExecutor just passes through to Invoker. | ||
var mockExecutor = new Mock<IFunctionExecutor>(); | ||
mockExecutor.Setup(m => m.TryExecuteAsync(It.IsAny<IFunctionInstance>(), It.IsAny<CancellationToken>())). | ||
Returns<IFunctionInstance, CancellationToken>((x, y) => | ||
{ | ||
sb.Append("2>"); | ||
x.Invoker.InvokeAsync(null).Wait(); | ||
sb.Append("<6"); | ||
return Task.FromResult<IDelayedException>(null); | ||
}); | ||
IFunctionExecutor executor = mockExecutor.Object; | ||
|
||
var mockInvoker = new Mock<IFunctionInvoker>(); | ||
mockInvoker.Setup(m => m.InvokeAsync(null)).Returns(() => | ||
{ | ||
sb.Append("4"); | ||
return Task.CompletedTask; | ||
} | ||
); | ||
IFunctionInvoker innerInvoker = mockInvoker.Object; | ||
|
||
IFunctionInstance inner = new FunctionInstance(Guid.NewGuid(), null, ExecutionReason.HostCall, null, innerInvoker, null); | ||
|
||
var mockInstanceFactory = new Mock<ITriggeredFunctionInstanceFactory<int>>(); | ||
mockInstanceFactory.Setup(m => m.Create(It.IsAny<int>(), null)).Returns(inner); | ||
ITriggeredFunctionInstanceFactory<int> instanceFactory = mockInstanceFactory.Object; | ||
|
||
var trigger = new TriggeredFunctionExecutor<int>(descr, executor, instanceFactory); | ||
|
||
var trigger2 = (ITriggeredFunctionExecutorWithHook)trigger; | ||
|
||
|
||
|
||
Func<Func<Task>, Task> hook = async (x) => { | ||
sb.Append("3>"); | ||
await x(); | ||
sb.Append("<5"); | ||
}; | ||
|
||
sb.Append("1>"); | ||
await trigger2.TryExecuteAsync(new TriggeredFunctionData { TriggerValue = 123 }, CancellationToken.None, hook); | ||
sb.Append("<7"); | ||
|
||
Assert.Equal("1>2>3>4<5<6<7", sb.ToString()); | ||
} | ||
} | ||
} |
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