Skip to content

Commit

Permalink
feat: allow multiple OnStarted and OnStopping callback
Browse files Browse the repository at this point in the history
  • Loading branch information
joelfoliveira committed Jun 17, 2024
1 parent 1bc8d2c commit 433f337
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/KafkaFlow/Configuration/ClusterConfigurationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ public IClusterConfigurationBuilder AddConsumer(Action<IConsumerConfigurationBui

public IClusterConfigurationBuilder OnStopping(Action<IDependencyResolver> handler)
{
_onStoppingHandler = handler;
_onStoppingHandler += handler;
return this;
}

public IClusterConfigurationBuilder OnStarted(Action<IDependencyResolver> handler)
{
_onStartedHandler = handler;
_onStartedHandler += handler;
return this;
}

Expand Down
75 changes: 66 additions & 9 deletions tests/KafkaFlow.IntegrationTests/GlobalEventsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class GlobalEventsTest
private readonly Fixture _fixture = new();
private string _topic;
private bool _isPartitionAssigned;
private IKafkaBus _bus;

[TestInitialize]
public void Setup()
Expand All @@ -35,6 +36,52 @@ public void Setup()
MessageStorage.Clear();
}

[TestMethod]
public async Task OnStarted_RegisterMultipleOnStartedCallbacks_AllAreCalled()
{
// Arrange
const int ExpectedOnStartedCount = 2;
var countOnStarted = 0;

// Act
await this.GetServiceProviderAsync(
observers => { },
this.ConfigureConsumer<GzipMiddleware>,
this.ConfigureProducer<ProtobufNetSerializer>,
cluster =>
{
cluster.OnStarted(_ => countOnStarted++);
cluster.OnStarted(_ => countOnStarted++);
});

// Assert
Assert.AreEqual(ExpectedOnStartedCount, countOnStarted);
}

[TestMethod]
public async Task OnStopping_RegisterMultipleOnStoppingCallbacks_AllAreCalled()
{
// Arrange
const int ExpectedOnStoppingCount = 2;
var countOnStopping = 0;

// Act
await this.GetServiceProviderAsync(
observers => { },
this.ConfigureConsumer<GzipMiddleware>,
this.ConfigureProducer<ProtobufNetSerializer>,
cluster =>
{
cluster.OnStopping(_ => countOnStopping++);
cluster.OnStopping(_ => countOnStopping++);
});

await _bus?.StopAsync();

// Assert
Assert.AreEqual(ExpectedOnStoppingCount, countOnStopping);
}

[TestMethod]
public async Task SubscribeGlobalEvents_AllEvents_TriggeredCorrectly()
{
Expand Down Expand Up @@ -241,10 +288,25 @@ private void ConfigureProducer<T>(IProducerConfigurationBuilder producerConfigur
private async Task<IServiceProvider> GetServiceProviderAsync(
Action<IGlobalEvents> configureGlobalEvents,
Action<IConsumerConfigurationBuilder> consumerConfiguration,
Action<IProducerConfigurationBuilder> producerConfiguration)
Action<IProducerConfigurationBuilder> producerConfiguration,
Action<IClusterConfigurationBuilder> builderConfiguration = null)
{
_isPartitionAssigned = false;

var clusterBuilderAction = (HostBuilderContext context, IClusterConfigurationBuilder cluster) =>
{
cluster
.WithBrokers(context.Configuration.GetValue<string>("Kafka:Brokers").Split(';'))
.CreateTopicIfNotExists(_topic, 1, 1)
.AddProducer<JsonProducer2>(producerConfiguration)
.AddConsumer(consumerConfiguration);
};

clusterBuilderAction += (_, cluster) =>
{
builderConfiguration?.Invoke(cluster);
};

var builder = Host
.CreateDefaultBuilder()
.ConfigureAppConfiguration(
Expand All @@ -262,12 +324,7 @@ private async Task<IServiceProvider> GetServiceProviderAsync(
services.AddKafka(
kafka => kafka
.UseLogHandler<TraceLogHandler>()
.AddCluster(
cluster => cluster
.WithBrokers(context.Configuration.GetValue<string>("Kafka:Brokers").Split(';'))
.CreateTopicIfNotExists(_topic, 1, 1)
.AddProducer<JsonProducer2>(producerConfiguration)
.AddConsumer(consumerConfiguration))
.AddCluster(cluster => { clusterBuilderAction.Invoke(context, cluster); })
.SubscribeGlobalEvents(configureGlobalEvents)))
.UseDefaultServiceProvider(
(_, options) =>
Expand All @@ -277,8 +334,8 @@ private async Task<IServiceProvider> GetServiceProviderAsync(
});

var host = builder.Build();
var bus = host.Services.CreateKafkaBus();
bus.StartAsync().GetAwaiter().GetResult();
_bus = host.Services.CreateKafkaBus();
_bus.StartAsync().GetAwaiter().GetResult();

await this.WaitForPartitionAssignmentAsync();

Expand Down

0 comments on commit 433f337

Please sign in to comment.