From d9891025711c4c3ddf00ddd79579b7c2876941fd Mon Sep 17 00:00:00 2001 From: Aishwarya Bhandari Date: Thu, 20 Jun 2024 12:45:34 -0700 Subject: [PATCH 1/3] initial change --- .../Context/Features/DefaultFunctionInputBindingFeature.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/DotNetWorker.Core/Context/Features/DefaultFunctionInputBindingFeature.cs b/src/DotNetWorker.Core/Context/Features/DefaultFunctionInputBindingFeature.cs index 2655a2fcb..58a143b96 100644 --- a/src/DotNetWorker.Core/Context/Features/DefaultFunctionInputBindingFeature.cs +++ b/src/DotNetWorker.Core/Context/Features/DefaultFunctionInputBindingFeature.cs @@ -29,7 +29,10 @@ public async ValueTask BindFunctionInputAsync(Functi { ObjectDisposedThrowHelper.ThrowIf(_disposed, this); - await _semaphoreSlim.WaitAsync(WaitTimeInMilliSeconds, context.CancellationToken); + // Setting second parameter to CancellationToken.None to prevent a TaskCancelledException if the + // pipeline cancels the code before the function is invoked. This way the customer code will be invoked + // and they can handle the cancellation token. + await _semaphoreSlim.WaitAsync(WaitTimeInMilliSeconds, CancellationToken.None); try { From f87e85fadcb07a3055d5938571a4c00cf7b7b3cc Mon Sep 17 00:00:00 2001 From: Aishwarya Bhandari Date: Wed, 26 Jun 2024 15:13:35 -0700 Subject: [PATCH 2/3] add unit test --- .../DefaultModelBindingFeatureTests.cs | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/test/DotNetWorkerTests/DefaultModelBindingFeatureTests.cs b/test/DotNetWorkerTests/DefaultModelBindingFeatureTests.cs index 4a1363aca..1dfeb1306 100644 --- a/test/DotNetWorkerTests/DefaultModelBindingFeatureTests.cs +++ b/test/DotNetWorkerTests/DefaultModelBindingFeatureTests.cs @@ -61,6 +61,47 @@ public async void BindFunctionInputAsync_Populates_ParametersUsingConverters() Assert.Equal("0ab4800e-1308-4e9f-be5f-4372717e68eb", guid.ToString()); } + [Fact] + public async void BindFunctionInputAsync_IgnoreCancellationToken() + { + // Arrange + var parameters = new List() + { + new("myQueueItem",typeof(Book)), + new ("myGuid", typeof(Guid)) + }; + IInvocationFeatures features = new InvocationFeatures(Enumerable.Empty()); + features.Set(_serviceProvider.GetService()); + features.Set(new TestFunctionBindingsFeature() + { + InputData = new Dictionary + { + { "myQueueItem","{\"id\":\"foo\", \"title\":\"bar\"}" }, + { "myGuid","0ab4800e-1308-4e9f-be5f-4372717e68eb" } + } + }); + + var definition = new TestFunctionDefinition(parameters: parameters, inputBindings: new Dictionary + { + { "myQueueItem", new TestBindingMetadata("myQueueItem","queueTrigger",BindingDirection.In) }, + { "myGuid", new TestBindingMetadata("myGuid","queueTrigger",BindingDirection.In) } + }); + + // Register a cancellation token + var cts = new CancellationTokenSource(); + cts.Cancel(); + var functionContext = new TestFunctionContext(definition, invocation: null, cts.Token, serviceProvider: _serviceProvider, features: features); + + // Act + var bindingResult = await _functionInputBindingFeature.BindFunctionInputAsync(functionContext); + var parameterValuesArray = bindingResult.Values; + // Assert + var book = TestUtility.AssertIsTypeAndConvert(parameterValuesArray[0]); + Assert.Equal("foo", book.Id); + var guid = TestUtility.AssertIsTypeAndConvert(parameterValuesArray[1]); + Assert.Equal("0ab4800e-1308-4e9f-be5f-4372717e68eb", guid.ToString()); + } + [Fact] public async void BindFunctionInputAsync_Populates_Parameter_Using_DefaultValue_When_CouldNot_Populate_From_InputData() { From 07d42393e853e52988fd779c89d189e90982901b Mon Sep 17 00:00:00 2001 From: Aishwarya Bhandari Date: Tue, 2 Jul 2024 14:13:37 -0700 Subject: [PATCH 3/3] updating unit test --- test/DotNetWorkerTests/DefaultModelBindingFeatureTests.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/DotNetWorkerTests/DefaultModelBindingFeatureTests.cs b/test/DotNetWorkerTests/DefaultModelBindingFeatureTests.cs index 1dfeb1306..be4bfb101 100644 --- a/test/DotNetWorkerTests/DefaultModelBindingFeatureTests.cs +++ b/test/DotNetWorkerTests/DefaultModelBindingFeatureTests.cs @@ -68,7 +68,8 @@ public async void BindFunctionInputAsync_IgnoreCancellationToken() var parameters = new List() { new("myQueueItem",typeof(Book)), - new ("myGuid", typeof(Guid)) + new ("myGuid", typeof(Guid)), + new ("token", typeof(CancellationToken)) }; IInvocationFeatures features = new InvocationFeatures(Enumerable.Empty()); features.Set(_serviceProvider.GetService()); @@ -100,6 +101,8 @@ public async void BindFunctionInputAsync_IgnoreCancellationToken() Assert.Equal("foo", book.Id); var guid = TestUtility.AssertIsTypeAndConvert(parameterValuesArray[1]); Assert.Equal("0ab4800e-1308-4e9f-be5f-4372717e68eb", guid.ToString()); + var cancellationToken = TestUtility.AssertIsTypeAndConvert(parameterValuesArray[2]); + Assert.True(cancellationToken.IsCancellationRequested); } [Fact]