Skip to content

Commit 4850cc1

Browse files
committed
Fix #1629 - default event hubs batch size is incorrect
It looks like when the event hubs extension was updated to the new model to parse host.json, the same code in the script layer was not removed, causing this issue. Added tests and also verified manually that this fix works.
1 parent 3c2a2be commit 4850cc1

File tree

2 files changed

+81
-26
lines changed

2 files changed

+81
-26
lines changed

src/WebJobs.Script/Binding/ServiceBusScriptBindingProvider.cs

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ namespace Microsoft.Azure.WebJobs.Script.Binding
1616
{
1717
internal class ServiceBusScriptBindingProvider : ScriptBindingProvider
1818
{
19-
private EventHubConfiguration _eventHubConfiguration;
20-
2119
public ServiceBusScriptBindingProvider(JobHostConfiguration config, JObject hostMetadata, TraceWriter traceWriter)
2220
: base(config, hostMetadata, traceWriter)
2321
{
@@ -65,32 +63,10 @@ public override void Initialize()
6563
}
6664
}
6765

68-
EventProcessorOptions eventProcessorOptions = EventProcessorOptions.DefaultOptions;
69-
eventProcessorOptions.MaxBatchSize = 1000;
70-
int batchCheckpointFrequency = 1;
71-
configSection = (JObject)Metadata.GetValue("eventHub", StringComparison.OrdinalIgnoreCase);
72-
if (configSection != null)
73-
{
74-
if (configSection.TryGetValue("maxBatchSize", StringComparison.OrdinalIgnoreCase, out value))
75-
{
76-
eventProcessorOptions.MaxBatchSize = (int)value;
77-
}
78-
79-
if (configSection.TryGetValue("prefetchCount", StringComparison.OrdinalIgnoreCase, out value))
80-
{
81-
eventProcessorOptions.PrefetchCount = (int)value;
82-
}
83-
84-
if (configSection.TryGetValue("batchCheckpointFrequency", StringComparison.OrdinalIgnoreCase, out value))
85-
{
86-
batchCheckpointFrequency = (int)value;
87-
}
88-
}
89-
_eventHubConfiguration = new EventHubConfiguration(eventProcessorOptions);
90-
_eventHubConfiguration.BatchCheckpointFrequency = batchCheckpointFrequency;
66+
var eventHubConfiguration = new EventHubConfiguration();
9167

9268
Config.UseServiceBus(serviceBusConfig);
93-
Config.UseEventHub(_eventHubConfiguration);
69+
Config.UseEventHub(eventHubConfiguration);
9470
}
9571

9672
public override bool TryResolveAssembly(string assemblyName, out Assembly assembly)

test/WebJobs.Script.Tests/ScriptHostTests.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
using Microsoft.Azure.WebJobs.Script.Description;
2424
using Microsoft.Azure.WebJobs.Script.Diagnostics;
2525
using Microsoft.Azure.WebJobs.Script.Eventing;
26+
using Microsoft.Azure.WebJobs.ServiceBus;
2627
using Microsoft.Extensions.Logging;
28+
using Microsoft.ServiceBus.Messaging;
2729
using Microsoft.WebJobs.Script.Tests;
2830
using Moq;
2931
using Newtonsoft.Json.Linq;
@@ -524,6 +526,83 @@ public void ApplyConfiguration_Http()
524526
Assert.Equal(10, httpConfig.MaxOutstandingRequests);
525527
}
526528

529+
[Fact]
530+
public void ApplyConfiguration_EventHubs_UsesWebJobsDefaults()
531+
{
532+
JObject config = new JObject();
533+
JObject eventHub = new JObject();
534+
config["eventHub"] = eventHub;
535+
536+
ScriptHostConfiguration scriptConfig = new ScriptHostConfiguration();
537+
scriptConfig.HostConfig.HostConfigMetadata = config;
538+
TraceWriter traceWriter = new TestTraceWriter(TraceLevel.Verbose);
539+
540+
var provider = new ServiceBusScriptBindingProvider(scriptConfig.HostConfig, config, traceWriter);
541+
provider.Initialize();
542+
543+
new JobHost(scriptConfig.HostConfig).CreateMetadataProvider(); // will cause extensions to initialize and consume config metadata.
544+
545+
IExtensionRegistry extensions = scriptConfig.HostConfig.GetService<IExtensionRegistry>();
546+
var eventHubConfig = extensions.GetExtensions<IExtensionConfigProvider>().OfType<EventHubConfiguration>().Single();
547+
548+
// I don't want to update any of the V1 public surface area, so I'll just use reflection for this test.
549+
var getOptionsMethod = typeof(EventHubConfiguration)
550+
.GetMethod("GetOptions", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
551+
552+
Assert.NotNull(getOptionsMethod);
553+
554+
var eventHubOptions = (EventProcessorOptions)getOptionsMethod.Invoke(eventHubConfig, new object[] { });
555+
556+
// Now lets find out what the defaults should be
557+
var defaultEventHubConfiguration = new EventHubConfiguration();
558+
var defaultOptions = (EventProcessorOptions)getOptionsMethod.Invoke(defaultEventHubConfiguration, new object[] { });
559+
560+
// And verify they match
561+
Assert.Equal(defaultOptions.MaxBatchSize, eventHubOptions.MaxBatchSize);
562+
Assert.Equal(defaultOptions.PrefetchCount, eventHubOptions.PrefetchCount);
563+
Assert.Equal(defaultEventHubConfiguration.BatchCheckpointFrequency, eventHubConfig.BatchCheckpointFrequency);
564+
}
565+
566+
[Fact]
567+
public void ApplyConfiguration_EventHubs_UsesCustomConfiguration()
568+
{
569+
JObject config = new JObject();
570+
JObject eventHub = new JObject();
571+
config["eventHub"] = eventHub;
572+
573+
var customBatchSize = 33;
574+
var customPrefetchCount = 99;
575+
var customCheckpointFrequency = 4;
576+
577+
eventHub["maxBatchSize"] = customBatchSize;
578+
eventHub["prefetchCount"] = customPrefetchCount;
579+
eventHub["batchCheckpointFrequency"] = customCheckpointFrequency;
580+
581+
ScriptHostConfiguration scriptConfig = new ScriptHostConfiguration();
582+
scriptConfig.HostConfig.HostConfigMetadata = config;
583+
TraceWriter traceWriter = new TestTraceWriter(TraceLevel.Verbose);
584+
585+
var provider = new ServiceBusScriptBindingProvider(scriptConfig.HostConfig, config, traceWriter);
586+
provider.Initialize();
587+
588+
new JobHost(scriptConfig.HostConfig).CreateMetadataProvider(); // will cause extensions to initialize and consume config metadata.
589+
590+
IExtensionRegistry extensions = scriptConfig.HostConfig.GetService<IExtensionRegistry>();
591+
var eventHubConfig = extensions.GetExtensions<IExtensionConfigProvider>().OfType<EventHubConfiguration>().Single();
592+
593+
// I don't want to update any of the V1 public surface area, so I'll just use reflection for this test.
594+
var getOptionsMethod = typeof(EventHubConfiguration)
595+
.GetMethod("GetOptions", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
596+
597+
Assert.NotNull(getOptionsMethod);
598+
599+
var eventHubOptions = (EventProcessorOptions)getOptionsMethod.Invoke(eventHubConfig, new object[] { });
600+
601+
Assert.Equal(customBatchSize, eventHubOptions.MaxBatchSize);
602+
Assert.Equal(customPrefetchCount, eventHubOptions.PrefetchCount);
603+
Assert.Equal(customCheckpointFrequency, eventHubConfig.BatchCheckpointFrequency);
604+
}
605+
527606
[Fact]
528607
public void ApplyConfiguration_Blobs()
529608
{

0 commit comments

Comments
 (0)