-
Notifications
You must be signed in to change notification settings - Fork 539
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
479 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
tests/Aspire.Kafka.Consumer.Tests/Aspire.Kafka.Consumer.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>$(NetCurrent)</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Components\Aspire.Kafka.Consumer\Aspire.Kafka.Consumer.csproj" /> | ||
<ProjectReference Include="..\Aspire.Components.Common.Tests\Aspire.Components.Common.Tests.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
136 changes: 136 additions & 0 deletions
136
tests/Aspire.Kafka.Consumer.Tests/AspireKafkaConsumerExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Text; | ||
using Confluent.Kafka; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Xunit; | ||
|
||
namespace Aspire.Kafka.Consumer.Tests; | ||
|
||
public class AspireKafkaConsumerExtensionsTests | ||
{ | ||
[ConditionalTheory] | ||
[InlineData(true)] | ||
[InlineData(false)] | ||
public void ReadsFromConnectionStringsCorrectly(bool useKeyed) | ||
{ | ||
var builder = Host.CreateEmptyApplicationBuilder(null); | ||
builder.Configuration.AddInMemoryCollection([ | ||
new KeyValuePair<string, string?>("ConnectionStrings:messaging", AspireKafkaConsumerHelpers.TestingEndpoint) | ||
]); | ||
|
||
if (useKeyed) | ||
{ | ||
builder.AddKeyedKafkaConsumer<string, string>("messaging"); | ||
} | ||
else | ||
{ | ||
builder.AddKafkaConsumer<string, string>("messaging"); | ||
} | ||
|
||
var host = builder.Build(); | ||
var ConsumerConfig = useKeyed ? | ||
host.Services.GetRequiredKeyedService<ConsumerConfig>("messaging") : | ||
host.Services.GetRequiredService<ConsumerConfig>(); | ||
|
||
Assert.Equal(AspireKafkaConsumerHelpers.TestingEndpoint, ConsumerConfig.BootstrapServers); | ||
} | ||
|
||
[ConditionalTheory] | ||
[InlineData(true)] | ||
[InlineData(false)] | ||
public void ConnectionStringCanBeSetInCode(bool useKeyed) | ||
{ | ||
var builder = Host.CreateEmptyApplicationBuilder(null); | ||
builder.Configuration.AddInMemoryCollection([ | ||
new KeyValuePair<string, string?>("ConnectionStrings:messaging", "unused") | ||
]); | ||
|
||
static void SetConnectionString(ConsumerConfig config) => config.BootstrapServers = AspireKafkaConsumerHelpers.TestingEndpoint; | ||
if (useKeyed) | ||
{ | ||
builder.AddKeyedKafkaConsumer<string, string>("messaging", configureConsumerConfig: SetConnectionString); | ||
} | ||
else | ||
{ | ||
builder.AddKafkaConsumer<string, string>("messaging", configureConsumerConfig: SetConnectionString); | ||
} | ||
|
||
var host = builder.Build(); | ||
var config = useKeyed ? | ||
host.Services.GetRequiredKeyedService<ConsumerConfig>("messaging") : | ||
host.Services.GetRequiredService<ConsumerConfig>(); | ||
|
||
Assert.Equal(AspireKafkaConsumerHelpers.TestingEndpoint, config.BootstrapServers); | ||
} | ||
|
||
[ConditionalTheory] | ||
[InlineData(true)] | ||
[InlineData(false)] | ||
public void ConnectionNameWinsOverConfigSection(bool useKeyed) | ||
{ | ||
var builder = Host.CreateEmptyApplicationBuilder(null); | ||
|
||
var key = useKeyed ? "redis" : null; | ||
builder.Configuration.AddInMemoryCollection([ | ||
new KeyValuePair<string, string?>(ConformanceTests.CreateConfigKey("Aspire:Kafka:Consumer", key, "ConnectionString"), "unused"), | ||
new KeyValuePair<string, string?>("ConnectionStrings:messaging", AspireKafkaConsumerHelpers.TestingEndpoint) | ||
]); | ||
|
||
if (useKeyed) | ||
{ | ||
builder.AddKeyedKafkaConsumer<string, string>("messaging"); | ||
} | ||
else | ||
{ | ||
builder.AddKafkaConsumer<string, string>("messaging"); | ||
} | ||
|
||
var host = builder.Build(); | ||
var config = useKeyed ? | ||
host.Services.GetRequiredKeyedService<ConsumerConfig>("messaging") : | ||
host.Services.GetRequiredService<ConsumerConfig>(); | ||
|
||
Assert.Equal(AspireKafkaConsumerHelpers.TestingEndpoint, config.BootstrapServers); | ||
} | ||
|
||
[Fact] | ||
public void ConsumerConfigOptionsFromConfig() | ||
{ | ||
static Stream CreateStreamFromString(string data) => new MemoryStream(Encoding.UTF8.GetBytes(data)); | ||
|
||
using var jsonStream = CreateStreamFromString(""" | ||
{ | ||
"Aspire": { | ||
"Kafka": { | ||
"Consumer": { | ||
"AutoOffsetReset": "Earliest", | ||
"SaslUsername": "user", | ||
"SaslPassword": "password", | ||
"SaslMechanism": "Plain", | ||
"SecurityProtocol": "Plaintext" | ||
} | ||
} | ||
} | ||
} | ||
"""); | ||
|
||
var builder = Host.CreateEmptyApplicationBuilder(null); | ||
|
||
builder.Configuration.AddJsonStream(jsonStream); | ||
|
||
builder.AddKafkaConsumer<string, string>("messaging"); | ||
|
||
var host = builder.Build(); | ||
var config = (ConsumerConfig)host.Services.GetRequiredService<ConsumerConfig>(); | ||
|
||
Assert.Equal(AutoOffsetReset.Earliest, config.AutoOffsetReset); | ||
Assert.Equal("user", config.SaslUsername); | ||
Assert.Equal("password", config.SaslPassword); | ||
Assert.Equal(SaslMechanism.Plain, config.SaslMechanism); | ||
Assert.Equal(SecurityProtocol.Plaintext, config.SecurityProtocol); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
tests/Aspire.Kafka.Consumer.Tests/AspireKafkaConsumerHelpers.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Aspire.Kafka.Consumer.Tests; | ||
|
||
internal sealed class AspireKafkaConsumerHelpers | ||
{ | ||
public const string TestingEndpoint = "localhost:9092"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Aspire.Components.ConformanceTests; | ||
using Confluent.Kafka; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace Aspire.Kafka.Consumer.Tests; | ||
internal sealed class ConformanceTests : ConformanceTests<ConsumerConfig, ConsumerConfig> | ||
{ | ||
protected override ServiceLifetime ServiceLifetime => throw new NotImplementedException(); | ||
|
||
protected override string ActivitySourceName => throw new NotImplementedException(); | ||
|
||
protected override string JsonSchemaPath => throw new NotImplementedException(); | ||
|
||
protected override string[] RequiredLogCategories => throw new NotImplementedException(); | ||
|
||
protected override void PopulateConfiguration(ConfigurationManager configuration, string? key = null) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
protected override void RegisterComponent(HostApplicationBuilder builder, Action<ConsumerConfig>? configure = null, string? key = null) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
protected override void SetHealthCheck(ConsumerConfig options, bool enabled) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
protected override void SetMetrics(ConsumerConfig options, bool enabled) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
protected override void SetTracing(ConsumerConfig options, bool enabled) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
protected override void TriggerActivity(ConsumerConfig service) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/Aspire.Kafka.Producer.Tests/Aspire.Kafka.Producer.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>$(NetCurrent)</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Components\Aspire.Kafka.Producer\Aspire.Kafka.Producer.csproj" /> | ||
<ProjectReference Include="..\Aspire.Components.Common.Tests\Aspire.Components.Common.Tests.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.