Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple OnStarted and OnStopping callbacks #573

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading