Skip to content

Commit d0380ea

Browse files
committed
[tests] Add Kakfa playground app to tests
1 parent a9e392e commit d0380ea

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

playground/kafka/Consumer/ConsumerWorker.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken)
1919
try
2020
{
2121
result = consumer.Consume(TimeSpan.FromSeconds(1));
22+
if (result is not null)
23+
{
24+
logger.LogInformation($"Consumed message [{result.Message?.Key}] = {result.Message?.Value}");
25+
}
2226
}
2327
catch (ConsumeException ex) when (ex.Error.Code == ErrorCode.UnknownTopicOrPart)
2428
{

tests/Aspire.Playground.Tests/Aspire.Playground.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<ProjectReference Include="$(PlaygroundSourceDir)Qdrant/Qdrant.AppHost/Qdrant.AppHost.csproj" />
4343
<ProjectReference Include="$(PlaygroundSourceDir)SqlServerEndToEnd/SqlServerEndToEnd.AppHost/SqlServerEndToEnd.AppHost.csproj" />
4444
<ProjectReference Include="$(PlaygroundSourceDir)TestShop/TestShop.AppHost/TestShop.AppHost.csproj" />
45+
<ProjectReference Include="$(PlaygroundSourceDir)kafka/KafkaBasic.AppHost/KafkaBasic.AppHost.csproj" />
4546
<ProjectReference Include="$(PlaygroundSourceDir)keycloak/Keycloak.AppHost/Keycloak.AppHost.csproj" />
4647
<ProjectReference Include="$(PlaygroundSourceDir)milvus/MilvusPlayground.AppHost/MilvusPlayground.AppHost.csproj" />
4748
<ProjectReference Include="$(PlaygroundSourceDir)withdockerfile/WithDockerfile.AppHost/WithDockerfile.AppHost.csproj" />

tests/Aspire.Playground.Tests/ProjectSpecificTests.cs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using Aspire.Hosting;
45
using Aspire.Hosting.Tests.Utils;
56
using SamplesIntegrationTests;
67
using SamplesIntegrationTests.Infrastructure;
78
using Xunit;
9+
using Xunit.Sdk;
810
using Xunit.Abstractions;
911

1012
namespace Aspire.Playground.Tests;
@@ -21,10 +23,59 @@ public async Task WithDockerfileTest()
2123
await app.StartAsync();
2224
await app.WaitForResources().WaitAsync(TimeSpan.FromMinutes(2));
2325

24-
await app.WaitForTextAsync([$"I'm Batman. - Batman", "I am Iron Man. - Iron Man"])
26+
await app.WaitForTextAsync($"I'm Batman. - Batman")
2527
.WaitAsync(TimeSpan.FromMinutes(3));
2628

2729
app.EnsureNoErrorsLogged();
2830
await app.StopAsync();
2931
}
32+
33+
[Fact]
34+
public async Task KafkaTest()
35+
{
36+
var appHostPath = Directory.GetFiles(AppContext.BaseDirectory, "KafkaBasic.AppHost.dll").Single();
37+
var appHost = await DistributedApplicationTestFactory.CreateAsync(appHostPath, _testOutput);
38+
await using var app = await appHost.BuildAsync();
39+
40+
await app.StartAsync();
41+
await app.WaitForResources().WaitAsync(TimeSpan.FromMinutes(2));
42+
43+
await WaitForAllTextAsync(app,
44+
[
45+
"Hello, World! 343",
46+
"Received 1000 messges."
47+
],
48+
timeoutSecs: 30);
49+
50+
app.EnsureNoErrorsLogged();
51+
await app.StopAsync();
52+
}
53+
54+
internal static async Task WaitForAllTextAsync(DistributedApplication app, IEnumerable<string> logTexts, string? resourceName = null, int timeoutSecs = -1)
55+
{
56+
var table = logTexts.ToList();
57+
try
58+
{
59+
var task = app.WaitForTextAsync((log) =>
60+
{
61+
foreach (var text in table)
62+
{
63+
if (log.Contains(text))
64+
{
65+
table.Remove(text);
66+
}
67+
}
68+
69+
return table.Count == 0;
70+
}, resourceName);
71+
72+
await (timeoutSecs > 0
73+
? task.WaitAsync(TimeSpan.FromSeconds(timeoutSecs))
74+
: task);
75+
}
76+
catch (TimeoutException te)
77+
{
78+
throw new XunitException($"The following messages were not found: {string.Join("', '", table)}", te);
79+
}
80+
}
3081
}

0 commit comments

Comments
 (0)