Skip to content

Commit

Permalink
Refactor ServiceBus converter & tests (#1716, #1723, #1727)
Browse files Browse the repository at this point in the history
  • Loading branch information
liliankasem committed Aug 1, 2023
1 parent 9043b7a commit f89ff7d
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
using Azure.Messaging.ServiceBus;
using Microsoft.Azure.Functions.Worker.Converters;
using Microsoft.Azure.Functions.Worker.Core;
using Microsoft.Azure.Functions.Worker.Extensions;
using Microsoft.Azure.Functions.Worker.Extensions.Abstractions;
using Microsoft.Azure.Functions.Worker.Extensions.ServiceBus;

namespace Microsoft.Azure.Functions.Worker
{
/// <summary>
/// Converter to bind to <see cref="ServiceBusReceivedMessage" /> or <see cref="ServiceBusReceivedMessage[]" /> type parameters.
/// </summary>
[SupportsDeferredBinding]
[SupportedConverterType(typeof(ServiceBusReceivedMessage))]
[SupportedConverterType(typeof(ServiceBusReceivedMessage[]))]
[SupportedTargetType(typeof(ServiceBusReceivedMessage))]
[SupportedTargetType(typeof(ServiceBusReceivedMessage[]))]
internal class ServiceBusReceivedMessageConverter : IInputConverter
{
public ValueTask<ConversionResult> ConvertAsync(ConverterContext context)
Expand All @@ -26,7 +30,7 @@ public ValueTask<ConversionResult> ConvertAsync(ConverterContext context)
{
ModelBindingData binding => ConversionResult.Success(ConvertToServiceBusReceivedMessage(binding)),
// Only array collections are currently supported, which matches the behavior of the in-proc extension.
CollectionModelBindingData collection => ConversionResult.Success(collection.ModelBindingDataArray
CollectionModelBindingData collection => ConversionResult.Success(collection.ModelBindingData
.Select(ConvertToServiceBusReceivedMessage).ToArray()),
_ => ConversionResult.Unhandled()
};
Expand All @@ -40,16 +44,19 @@ public ValueTask<ConversionResult> ConvertAsync(ConverterContext context)

private ServiceBusReceivedMessage ConvertToServiceBusReceivedMessage(ModelBindingData binding)
{
if (binding?.Source is not Constants.BindingSource)
if (binding is null)
{
throw new InvalidOperationException(
$"Unexpected binding source. Only '{Constants.BindingSource}' is supported.");
throw new ArgumentNullException(nameof(binding));
}

if (binding.ContentType != Constants.BinaryContentType)
if (binding.Source is not Constants.BindingSource)
{
throw new InvalidOperationException(
$"Unexpected content-type. Only '{Constants.BinaryContentType}' is supported.");
throw new InvalidBindingSourceException(binding.Source, Constants.BindingSource);
}

if (binding.ContentType is not Constants.BinaryContentType)
{
throw new InvalidContentTypeException(binding.ContentType, Constants.BinaryContentType);
}

// The lock token is a 16 byte GUID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

namespace Microsoft.Azure.Functions.Worker
{
[AllowConverterFallback(true)]
[InputConverter(typeof(ServiceBusReceivedMessageConverter))]
[ConverterFallbackBehavior(ConverterFallbackBehavior.Default)]
public sealed class ServiceBusTriggerAttribute : TriggerBindingAttribute, ISupportCardinality
{
private bool _isBatched = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</ItemGroup>

<ItemGroup>
<SharedReference Include="..\..\Worker.Extensions.Shared/Worker.Extensions.Shared.csproj" />
<SharedReference Include="..\..\Worker.Extensions.Shared\Worker.Extensions.Shared.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion samples/WorkerBindingSamples/WorkerBindingSamples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.12.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.13.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
<PackageReference Include="System.Net.NameResolution" Version="4.3.0" />
</ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion samples/WorkerBindingSamples/local.settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"CosmosDBConnection": "",
"EventHubConnection": ""
"EventHubConnection": "",
"ServiceBusConnection": ""
}
}
1 change: 0 additions & 1 deletion test/E2ETests/E2EApps/E2EApp/E2EApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\extensions\Worker.Extensions.ServiceBus\src\Worker.Extensions.ServiceBus.csproj" />
<ProjectReference Include="..\..\..\..\extensions\Worker.Extensions.Storage\src\Worker.Extensions.Storage.csproj" />
<ProjectReference Include="..\..\..\..\extensions\Worker.Extensions.Abstractions\src\Worker.Extensions.Abstractions.csproj" />
<ProjectReference Include="..\..\..\..\extensions\Worker.Extensions.CosmosDB\src\Worker.Extensions.CosmosDB.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -855,5 +855,71 @@
}
}
]
},
{
"name": "ServiceBusReceivedMessageFunction",
"scriptFile": "WorkerBindingSamples.dll",
"entryPoint": "SampleApp.ServiceBusReceivedMessageBindingSamples.ServiceBusReceivedMessageFunction",
"language": "dotnet-isolated",
"properties": {
"IsCodeless": false
},
"bindings": [
{
"name": "message",
"direction": "In",
"type": "serviceBusTrigger",
"queueName": "queue",
"connection": "ServiceBusConnection",
"cardinality": "One",
"properties": {
"supportsDeferredBinding": "True"
}
}
]
},
{
"name": "ServiceBusReceivedMessageBatchFunction",
"scriptFile": "WorkerBindingSamples.dll",
"entryPoint": "SampleApp.ServiceBusReceivedMessageBindingSamples.ServiceBusReceivedMessageBatchFunction",
"language": "dotnet-isolated",
"properties": {
"IsCodeless": false
},
"bindings": [
{
"name": "messages",
"direction": "In",
"type": "serviceBusTrigger",
"queueName": "queue",
"connection": "ServiceBusConnection",
"cardinality": "Many",
"properties": {
"supportsDeferredBinding": "True"
}
}
]
},
{
"name": "ServiceBusReceivedMessageWithStringProperties",
"scriptFile": "WorkerBindingSamples.dll",
"entryPoint": "SampleApp.ServiceBusReceivedMessageBindingSamples.ServiceBusReceivedMessageWithStringProperties",
"language": "dotnet-isolated",
"properties": {
"IsCodeless": false
},
"bindings": [
{
"name": "message",
"direction": "In",
"type": "serviceBusTrigger",
"queueName": "queue",
"connection": "ServiceBusConnection",
"cardinality": "One",
"properties": {
"supportsDeferredBinding": "True"
}
}
]
}
]
3 changes: 3 additions & 0 deletions test/SdkE2ETests/PublishTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ private async Task RunPublishTestForSdkTypeBindings(string outputDir, string add
new Extension("Startup",
"Microsoft.Azure.WebJobs.Extensions.FunctionMetadataLoader.Startup, Microsoft.Azure.WebJobs.Extensions.FunctionMetadataLoader, Version=1.0.0.0, Culture=neutral, PublicKeyToken=551316b6919f366c",
@"./.azurefunctions/Microsoft.Azure.WebJobs.Extensions.FunctionMetadataLoader.dll"),
new Extension("ServiceBus",
"Microsoft.Azure.WebJobs.ServiceBus.ServiceBusWebJobsStartup, Microsoft.Azure.WebJobs.Extensions.ServiceBus, Version=5.11.0.0, Culture=neutral, PublicKeyToken=92742159e12e44c8",
@"./.azurefunctions/Microsoft.Azure.WebJobs.Extensions.ServiceBus.dll"),
new Extension("AzureStorageBlobs",
"Microsoft.Azure.WebJobs.Extensions.Storage.AzureStorageBlobsWebJobsStartup, Microsoft.Azure.WebJobs.Extensions.Storage.Blobs, Version=5.1.3.0, Culture=neutral, PublicKeyToken=92742159e12e44c8",
@"./.azurefunctions/Microsoft.Azure.WebJobs.Extensions.Storage.Blobs.dll"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using Microsoft.Azure.Functions.Worker.Tests.Converters;
using Xunit;

namespace Microsoft.Azure.Functions.WorkerExtension.Tests
namespace Microsoft.Azure.Functions.Worker.Extensions.Tests
{
public class ServiceBusReceivedMessageConverterTests
{
Expand Down Expand Up @@ -89,7 +89,7 @@ public async Task ConvertAsync_ReturnsFailure_WrongContentType()
Assert.Equal(ConversionStatus.Failed, result.Status);
var output = result.Value as ServiceBusReceivedMessage;
Assert.Null(output);
Assert.IsType<InvalidOperationException>(result.Error);
Assert.Equal("Unexpected content-type 'application/json'. Only 'application/octet-stream' is supported.", result.Error.Message);
}

[Fact]
Expand Down Expand Up @@ -117,7 +117,7 @@ public async Task ConvertAsync_Batch_ReturnsFailure_WrongContentType()
Assert.Equal(ConversionStatus.Failed, result.Status);
var output = result.Value as ServiceBusReceivedMessage[];
Assert.Null(output);
Assert.IsType<InvalidOperationException>(result.Error);
Assert.Equal("Unexpected content-type 'application/json'. Only 'application/octet-stream' is supported.", result.Error.Message);
}

[Fact]
Expand All @@ -140,7 +140,7 @@ public async Task ConvertAsync_ReturnsFailure_WrongSource()
Assert.Equal(ConversionStatus.Failed, result.Status);
var output = result.Value as ServiceBusReceivedMessage;
Assert.Null(output);
Assert.IsType<InvalidOperationException>(result.Error);
Assert.Equal("Unexpected binding source 'some-other-source'. Only 'AzureServiceBusReceivedMessage' is supported.", result.Error.Message);
}

[Fact]
Expand Down Expand Up @@ -168,7 +168,7 @@ public async Task ConvertAsync_Batch_ReturnsFailure_WrongSource()
Assert.Equal(ConversionStatus.Failed, result.Status);
var output = result.Value as ServiceBusReceivedMessage[];
Assert.Null(output);
Assert.IsType<InvalidOperationException>(result.Error);
Assert.Equal("Unexpected binding source 'some-other-source'. Only 'AzureServiceBusReceivedMessage' is supported.", result.Error.Message);
}

private static void AssertReceivedMessage(ServiceBusReceivedMessage output, Guid lockToken)
Expand Down

0 comments on commit f89ff7d

Please sign in to comment.