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 12, 2024
1 parent 1bc8d2c commit 97493a3
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 11 deletions.
1 change: 1 addition & 0 deletions dass-core
Submodule dass-core added at 96c0f3
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
var provider = await this.GetServiceProviderAsync(

Check notice on line 47 in tests/KafkaFlow.IntegrationTests/GlobalEventsTest.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/KafkaFlow.IntegrationTests/GlobalEventsTest.cs#L47

Remove the unused local variable 'provider'.
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
var provider = 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 += (HostBuilderContext context, IClusterConfigurationBuilder cluster) =>

Check notice on line 305 in tests/KafkaFlow.IntegrationTests/GlobalEventsTest.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/KafkaFlow.IntegrationTests/GlobalEventsTest.cs#L305

'context' is not used. Use discard parameter instead.
{
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 97493a3

Please sign in to comment.