Skip to content

Commit f89ff7d

Browse files
committed
Refactor ServiceBus converter & tests (#1716, #1723, #1727)
1 parent 9043b7a commit f89ff7d

File tree

9 files changed

+95
-19
lines changed

9 files changed

+95
-19
lines changed

extensions/Worker.Extensions.ServiceBus/src/ServiceBusReceivedMessageConverter.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@
88
using Azure.Messaging.ServiceBus;
99
using Microsoft.Azure.Functions.Worker.Converters;
1010
using Microsoft.Azure.Functions.Worker.Core;
11+
using Microsoft.Azure.Functions.Worker.Extensions;
1112
using Microsoft.Azure.Functions.Worker.Extensions.Abstractions;
1213
using Microsoft.Azure.Functions.Worker.Extensions.ServiceBus;
1314

1415
namespace Microsoft.Azure.Functions.Worker
1516
{
17+
/// <summary>
18+
/// Converter to bind to <see cref="ServiceBusReceivedMessage" /> or <see cref="ServiceBusReceivedMessage[]" /> type parameters.
19+
/// </summary>
1620
[SupportsDeferredBinding]
17-
[SupportedConverterType(typeof(ServiceBusReceivedMessage))]
18-
[SupportedConverterType(typeof(ServiceBusReceivedMessage[]))]
21+
[SupportedTargetType(typeof(ServiceBusReceivedMessage))]
22+
[SupportedTargetType(typeof(ServiceBusReceivedMessage[]))]
1923
internal class ServiceBusReceivedMessageConverter : IInputConverter
2024
{
2125
public ValueTask<ConversionResult> ConvertAsync(ConverterContext context)
@@ -26,7 +30,7 @@ public ValueTask<ConversionResult> ConvertAsync(ConverterContext context)
2630
{
2731
ModelBindingData binding => ConversionResult.Success(ConvertToServiceBusReceivedMessage(binding)),
2832
// Only array collections are currently supported, which matches the behavior of the in-proc extension.
29-
CollectionModelBindingData collection => ConversionResult.Success(collection.ModelBindingDataArray
33+
CollectionModelBindingData collection => ConversionResult.Success(collection.ModelBindingData
3034
.Select(ConvertToServiceBusReceivedMessage).ToArray()),
3135
_ => ConversionResult.Unhandled()
3236
};
@@ -40,16 +44,19 @@ public ValueTask<ConversionResult> ConvertAsync(ConverterContext context)
4044

4145
private ServiceBusReceivedMessage ConvertToServiceBusReceivedMessage(ModelBindingData binding)
4246
{
43-
if (binding?.Source is not Constants.BindingSource)
47+
if (binding is null)
4448
{
45-
throw new InvalidOperationException(
46-
$"Unexpected binding source. Only '{Constants.BindingSource}' is supported.");
49+
throw new ArgumentNullException(nameof(binding));
4750
}
4851

49-
if (binding.ContentType != Constants.BinaryContentType)
52+
if (binding.Source is not Constants.BindingSource)
5053
{
51-
throw new InvalidOperationException(
52-
$"Unexpected content-type. Only '{Constants.BinaryContentType}' is supported.");
54+
throw new InvalidBindingSourceException(binding.Source, Constants.BindingSource);
55+
}
56+
57+
if (binding.ContentType is not Constants.BinaryContentType)
58+
{
59+
throw new InvalidContentTypeException(binding.ContentType, Constants.BinaryContentType);
5360
}
5461

5562
// The lock token is a 16 byte GUID

extensions/Worker.Extensions.ServiceBus/src/ServiceBusTriggerAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
namespace Microsoft.Azure.Functions.Worker
1010
{
11-
[AllowConverterFallback(true)]
1211
[InputConverter(typeof(ServiceBusReceivedMessageConverter))]
12+
[ConverterFallbackBehavior(ConverterFallbackBehavior.Default)]
1313
public sealed class ServiceBusTriggerAttribute : TriggerBindingAttribute, ISupportCardinality
1414
{
1515
private bool _isBatched = false;

extensions/Worker.Extensions.ServiceBus/src/Worker.Extensions.ServiceBus.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
</ItemGroup>
2727

2828
<ItemGroup>
29-
<SharedReference Include="..\..\Worker.Extensions.Shared/Worker.Extensions.Shared.csproj" />
29+
<SharedReference Include="..\..\Worker.Extensions.Shared\Worker.Extensions.Shared.csproj" />
3030
</ItemGroup>
3131

3232
</Project>

samples/WorkerBindingSamples/WorkerBindingSamples.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99
<ItemGroup>
10-
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.12.0" />
10+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.13.0" />
1111
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
1212
<PackageReference Include="System.Net.NameResolution" Version="4.3.0" />
1313
</ItemGroup>

samples/WorkerBindingSamples/local.settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
55
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
66
"CosmosDBConnection": "",
7-
"EventHubConnection": ""
7+
"EventHubConnection": "",
8+
"ServiceBusConnection": ""
89
}
910
}

test/E2ETests/E2EApps/E2EApp/E2EApp.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
</PropertyGroup>
1919

2020
<ItemGroup>
21-
<ProjectReference Include="..\..\..\..\extensions\Worker.Extensions.ServiceBus\src\Worker.Extensions.ServiceBus.csproj" />
2221
<ProjectReference Include="..\..\..\..\extensions\Worker.Extensions.Storage\src\Worker.Extensions.Storage.csproj" />
2322
<ProjectReference Include="..\..\..\..\extensions\Worker.Extensions.Abstractions\src\Worker.Extensions.Abstractions.csproj" />
2423
<ProjectReference Include="..\..\..\..\extensions\Worker.Extensions.CosmosDB\src\Worker.Extensions.CosmosDB.csproj" />

test/SdkE2ETests/Contents/WorkerBindingSamplesOutput/functions.metadata

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,5 +855,71 @@
855855
}
856856
}
857857
]
858+
},
859+
{
860+
"name": "ServiceBusReceivedMessageFunction",
861+
"scriptFile": "WorkerBindingSamples.dll",
862+
"entryPoint": "SampleApp.ServiceBusReceivedMessageBindingSamples.ServiceBusReceivedMessageFunction",
863+
"language": "dotnet-isolated",
864+
"properties": {
865+
"IsCodeless": false
866+
},
867+
"bindings": [
868+
{
869+
"name": "message",
870+
"direction": "In",
871+
"type": "serviceBusTrigger",
872+
"queueName": "queue",
873+
"connection": "ServiceBusConnection",
874+
"cardinality": "One",
875+
"properties": {
876+
"supportsDeferredBinding": "True"
877+
}
878+
}
879+
]
880+
},
881+
{
882+
"name": "ServiceBusReceivedMessageBatchFunction",
883+
"scriptFile": "WorkerBindingSamples.dll",
884+
"entryPoint": "SampleApp.ServiceBusReceivedMessageBindingSamples.ServiceBusReceivedMessageBatchFunction",
885+
"language": "dotnet-isolated",
886+
"properties": {
887+
"IsCodeless": false
888+
},
889+
"bindings": [
890+
{
891+
"name": "messages",
892+
"direction": "In",
893+
"type": "serviceBusTrigger",
894+
"queueName": "queue",
895+
"connection": "ServiceBusConnection",
896+
"cardinality": "Many",
897+
"properties": {
898+
"supportsDeferredBinding": "True"
899+
}
900+
}
901+
]
902+
},
903+
{
904+
"name": "ServiceBusReceivedMessageWithStringProperties",
905+
"scriptFile": "WorkerBindingSamples.dll",
906+
"entryPoint": "SampleApp.ServiceBusReceivedMessageBindingSamples.ServiceBusReceivedMessageWithStringProperties",
907+
"language": "dotnet-isolated",
908+
"properties": {
909+
"IsCodeless": false
910+
},
911+
"bindings": [
912+
{
913+
"name": "message",
914+
"direction": "In",
915+
"type": "serviceBusTrigger",
916+
"queueName": "queue",
917+
"connection": "ServiceBusConnection",
918+
"cardinality": "One",
919+
"properties": {
920+
"supportsDeferredBinding": "True"
921+
}
922+
}
923+
]
858924
}
859925
]

test/SdkE2ETests/PublishTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ private async Task RunPublishTestForSdkTypeBindings(string outputDir, string add
130130
new Extension("Startup",
131131
"Microsoft.Azure.WebJobs.Extensions.FunctionMetadataLoader.Startup, Microsoft.Azure.WebJobs.Extensions.FunctionMetadataLoader, Version=1.0.0.0, Culture=neutral, PublicKeyToken=551316b6919f366c",
132132
@"./.azurefunctions/Microsoft.Azure.WebJobs.Extensions.FunctionMetadataLoader.dll"),
133+
new Extension("ServiceBus",
134+
"Microsoft.Azure.WebJobs.ServiceBus.ServiceBusWebJobsStartup, Microsoft.Azure.WebJobs.Extensions.ServiceBus, Version=5.11.0.0, Culture=neutral, PublicKeyToken=92742159e12e44c8",
135+
@"./.azurefunctions/Microsoft.Azure.WebJobs.Extensions.ServiceBus.dll"),
133136
new Extension("AzureStorageBlobs",
134137
"Microsoft.Azure.WebJobs.Extensions.Storage.AzureStorageBlobsWebJobsStartup, Microsoft.Azure.WebJobs.Extensions.Storage.Blobs, Version=5.1.3.0, Culture=neutral, PublicKeyToken=92742159e12e44c8",
135138
@"./.azurefunctions/Microsoft.Azure.WebJobs.Extensions.Storage.Blobs.dll"),

test/Worker.Extensions.Tests/ServiceBus/ServiceBusReceivedMessageConverterTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
using Microsoft.Azure.Functions.Worker.Tests.Converters;
1313
using Xunit;
1414

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

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

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

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

174174
private static void AssertReceivedMessage(ServiceBusReceivedMessage output, Guid lockToken)

0 commit comments

Comments
 (0)