Skip to content

xunit1051 Part 1 #8951

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

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 1 addition & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@

<!-- Issue: https://github.com/dotnet/aspire/issues/8488 -->
<!-- xUnit2031: Do not use Where clause with Assert.Single -->
<!-- xUnit1051: Calls to methods which accept CancellationToken should use TestContext.Current.CancellationToken to allow test cancellation to be more responsive. -->
<!-- TODO: Re-enable and remove this. -->
<NoWarn>$(NoWarn);xUnit2031;xUnit1051</NoWarn>
<NoWarn>$(NoWarn);xUnit2031;</NoWarn>
</PropertyGroup>

<!-- OS/Architecture properties for local development resources -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public async Task CanConfigurePipelineAsync(bool useKeyed)
host.Services.GetRequiredKeyedService<IChatClient>("openai_chatclient") :
host.Services.GetRequiredService<IChatClient>();

var completion = await client.GetResponseAsync("Whatever");
var completion = await client.GetResponseAsync("Whatever", cancellationToken: TestContext.Current.CancellationToken);
Assert.Equal("Hello from middleware", completion.Text);

static Task<ChatResponse> TestMiddleware(IEnumerable<ChatMessage> list, ChatOptions? options, IChatClient client, CancellationToken token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public async Task CanConfigurePipelineAsync(bool useKeyed)
host.Services.GetRequiredKeyedService<IEmbeddingGenerator<string, Embedding<float>>>("openai_embeddinggenerator") :
host.Services.GetRequiredService<IEmbeddingGenerator<string, Embedding<float>>>();

var vector = await generator.GenerateEmbeddingVectorAsync("Hello");
var vector = await generator.GenerateEmbeddingVectorAsync("Hello", cancellationToken: TestContext.Current.CancellationToken);
Assert.Equal(1.23f, vector.ToArray().Single());
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Aspire.Cli.Tests/Aspire.Cli.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(DefaultTargetFramework)</TargetFramework>
Expand All @@ -9,6 +9,7 @@
<!-- Do not run tests in Helix at all -->
<RunOnAzdoHelixWindows>false</RunOnAzdoHelixWindows>
<RunOnAzdoHelixLinux>false</RunOnAzdoHelixLinux>
<NoWarn>$(NoWarn);xUnit1051</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down
5 changes: 3 additions & 2 deletions tests/Aspire.Components.Common.Tests/ConformanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ await healthCheckService.CheckHealthAsync(healthCheckRegistration =>
{
registeredNames.Add(healthCheckRegistration.Name);
return false;
}).ConfigureAwait(false);
},
TestContext.Current.CancellationToken).ConfigureAwait(false);
#pragma warning restore xUnit1030 // Do not call ConfigureAwait(false) in test method

Assert.Equal(2, registeredNames.Count);
Expand Down Expand Up @@ -320,7 +321,7 @@ public async Task HealthCheckReportsExpectedStatus(string? key)
HealthCheckService healthCheckService = host.Services.GetRequiredService<HealthCheckService>();

#pragma warning disable xUnit1030 // Do not call ConfigureAwait(false) in test method
HealthReport healthReport = await healthCheckService.CheckHealthAsync().ConfigureAwait(false);
HealthReport healthReport = await healthCheckService.CheckHealthAsync(TestContext.Current.CancellationToken).ConfigureAwait(false);
#pragma warning restore xUnit1030 // Do not call ConfigureAwait(false) in test method

HealthStatus expected = CanConnectToServer ? HealthStatus.Healthy : HealthStatus.Unhealthy;
Expand Down
36 changes: 18 additions & 18 deletions tests/Aspire.Confluent.Kafka.Tests/Aspire8MetricsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async Task ExposesStatisticsAsCountersAndGauge_InitializeCounters(TestVar
}

using var host = builder.Build();
await host.StartAsync();
await host.StartAsync(TestContext.Current.CancellationToken);

var metricsChannel = host.Services.GetRequiredService(ReflectionHelpers.MetricsChannelType.Value);
var writer = GetMetricsChannelWriter(metricsChannel)!;
Expand All @@ -61,14 +61,14 @@ public async Task ExposesStatisticsAsCountersAndGauge_InitializeCounters(TestVar
}

await Task.WhenAll(
collectorNetworkTx.WaitForMeasurementsAsync(statistics.Count),
collectorNetworkTransmitted.WaitForMeasurementsAsync(statistics.Count),
collectorNetworkRx.WaitForMeasurementsAsync(statistics.Count),
collectorNetworkReceived.WaitForMeasurementsAsync(statistics.Count),
collectorMessageTx.WaitForMeasurementsAsync(statistics.Count),
collectorMessageTransmitted.WaitForMeasurementsAsync(statistics.Count),
collectorMessageRx.WaitForMeasurementsAsync(statistics.Count),
collectorMessageReceived.WaitForMeasurementsAsync(statistics.Count)
collectorNetworkTx.WaitForMeasurementsAsync(statistics.Count, TestContext.Current.CancellationToken),
collectorNetworkTransmitted.WaitForMeasurementsAsync(statistics.Count, TestContext.Current.CancellationToken),
collectorNetworkRx.WaitForMeasurementsAsync(statistics.Count, TestContext.Current.CancellationToken),
collectorNetworkReceived.WaitForMeasurementsAsync(statistics.Count, TestContext.Current.CancellationToken),
collectorMessageTx.WaitForMeasurementsAsync(statistics.Count, TestContext.Current.CancellationToken),
collectorMessageTransmitted.WaitForMeasurementsAsync(statistics.Count, TestContext.Current.CancellationToken),
collectorMessageRx.WaitForMeasurementsAsync(statistics.Count, TestContext.Current.CancellationToken),
collectorMessageReceived.WaitForMeasurementsAsync(statistics.Count, TestContext.Current.CancellationToken)
);

collectorConsumerQueueMessageCount.RecordObservableInstruments();
Expand Down Expand Up @@ -153,7 +153,7 @@ public async Task ExposesStatisticsAsCountersAndGauge_AggregateCountersByName(Te
}

using var host = builder.Build();
await host.StartAsync();
await host.StartAsync(TestContext.Current.CancellationToken);

var metricsChannel = host.Services.GetRequiredService(ReflectionHelpers.MetricsChannelType.Value);
var writer = GetMetricsChannelWriter(metricsChannel)!;
Expand All @@ -176,14 +176,14 @@ public async Task ExposesStatisticsAsCountersAndGauge_AggregateCountersByName(Te
}

await Task.WhenAll(
collectorNetworkTx.WaitForMeasurementsAsync(statistics.Count),
collectorNetworkTransmitted.WaitForMeasurementsAsync(statistics.Count),
collectorNetworkRx.WaitForMeasurementsAsync(statistics.Count),
collectorNetworkReceived.WaitForMeasurementsAsync(statistics.Count),
collectorMessageTx.WaitForMeasurementsAsync(statistics.Count),
collectorMessageTransmitted.WaitForMeasurementsAsync(statistics.Count),
collectorMessageRx.WaitForMeasurementsAsync(statistics.Count),
collectorMessageReceived.WaitForMeasurementsAsync(statistics.Count)
collectorNetworkTx.WaitForMeasurementsAsync(statistics.Count, TestContext.Current.CancellationToken),
collectorNetworkTransmitted.WaitForMeasurementsAsync(statistics.Count, TestContext.Current.CancellationToken),
collectorNetworkRx.WaitForMeasurementsAsync(statistics.Count, TestContext.Current.CancellationToken),
collectorNetworkReceived.WaitForMeasurementsAsync(statistics.Count, TestContext.Current.CancellationToken),
collectorMessageTx.WaitForMeasurementsAsync(statistics.Count, TestContext.Current.CancellationToken),
collectorMessageTransmitted.WaitForMeasurementsAsync(statistics.Count, TestContext.Current.CancellationToken),
collectorMessageRx.WaitForMeasurementsAsync(statistics.Count, TestContext.Current.CancellationToken),
collectorMessageReceived.WaitForMeasurementsAsync(statistics.Count, TestContext.Current.CancellationToken)
);

collectorConsumerQueueMessageCount.RecordObservableInstruments();
Expand Down
6 changes: 3 additions & 3 deletions tests/Aspire.Confluent.Kafka.Tests/OtelMetricsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public async Task EnsureMetricsAreProducedAsync(bool useKeyed)
builder.Services.AddOpenTelemetry().WithMetrics(meterProvider => meterProvider.AddInMemoryExporter(metrics));

using var host = builder.Build();
await host.StartAsync();
await host.StartAsync(TestContext.Current.CancellationToken);
IGrouping<string, Metric>[] groups;

string topic = $"otel-topic-{Guid.NewGuid()}";
Expand Down Expand Up @@ -90,7 +90,7 @@ public async Task EnsureMetricsAreProducedAsync(bool useKeyed)
int j = 0;
while (true)
{
var consumerResult = consumer.Consume();
var consumerResult = consumer.Consume(TestContext.Current.CancellationToken);
if (consumerResult == null)
{
continue;
Expand All @@ -108,7 +108,7 @@ public async Task EnsureMetricsAreProducedAsync(bool useKeyed)

host.Services.GetRequiredService<MeterProvider>().EnsureMetricsAreFlushed();

await host.StopAsync();
await host.StopAsync(TestContext.Current.CancellationToken);

groups = metrics.Where(x => x.MeterName == "OpenTelemetry.Instrumentation.ConfluentKafka")
.GroupBy(x => x.Name).ToArray();
Expand Down
6 changes: 3 additions & 3 deletions tests/Aspire.Confluent.Kafka.Tests/OtelTracesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public async Task EnsureTracesAreProducedAsync(bool useKeyed)
builder.Services.AddOpenTelemetry().WithTracing(traceProviderBuilder => traceProviderBuilder.AddInMemoryExporter(activities));

using var host = builder.Build();
await host.StartAsync();
await host.StartAsync(TestContext.Current.CancellationToken);

string topic = $"otel-topic-{Guid.NewGuid()}";
using (var producer = useKeyed
Expand Down Expand Up @@ -95,7 +95,7 @@ public async Task EnsureTracesAreProducedAsync(bool useKeyed)
int j = 0;
while (true)
{
var consumerResult = consumer.Consume();
var consumerResult = consumer.Consume(TestContext.Current.CancellationToken);
if (consumerResult == null)
{
continue;
Expand All @@ -113,6 +113,6 @@ public async Task EnsureTracesAreProducedAsync(bool useKeyed)

Assert.Equal(5, activities.Where(x => x.OperationName == $"{topic} receive").Count());

await host.StopAsync();
await host.StopAsync(TestContext.Current.CancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
using Microsoft.FluentUI.AspNetCore.Components;
using Microsoft.FluentUI.AspNetCore.Components.Components.Tooltip;
using Xunit;

using TestContext = Xunit.TestContext;

namespace Aspire.Dashboard.Components.Tests.Layout;

[UseCulture("en-US")]
Expand Down Expand Up @@ -72,13 +73,13 @@ public async Task OnInitialize_UnsecuredOtlp_NotDismissed_DisplayMessageBar()
});

// Assert
await messageShownTcs.Task.WaitAsync(TimeSpan.FromSeconds(5));
await messageShownTcs.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);

Assert.NotNull(message);

message.Close();

Assert.True(await dismissedSettingSetTcs.Task.WaitAsync(TimeSpan.FromSeconds(5)));
Assert.True(await dismissedSettingSetTcs.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken));
}

[Fact]
Expand Down Expand Up @@ -116,8 +117,8 @@ public async Task OnInitialize_UnsecuredOtlp_Dismissed_NoMessageBar()
});

// Assert
var timeoutTask = Task.Delay(100);
var completedTask = await Task.WhenAny(messageShownTcs.Task, timeoutTask).WaitAsync(TimeSpan.FromSeconds(5));
var timeoutTask = Task.Delay(100, TestContext.Current.CancellationToken);
var completedTask = await Task.WhenAny(messageShownTcs.Task, timeoutTask).WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);

// It's hard to test something not happening.
// In this case of checking for a message, apply a small display and then double check that no message was displayed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using Microsoft.Extensions.Options;
using Microsoft.FluentUI.AspNetCore.Components;
using Xunit;
using TestContext = Xunit.TestContext;

namespace Aspire.Dashboard.Components.Tests.Pages;

Expand Down Expand Up @@ -91,7 +92,7 @@ public async Task ResourceName_SubscribeOnLoadAndChange_SubscribeConsoleLogsOnce
logger.LogInformation("Waiting for finish message.");
cut.WaitForState(() => instance.PageViewModel.Status == loc[nameof(Resources.ConsoleLogs.ConsoleLogsFinishedWatchingLogs)]);

var subscribedResourceName1 = await subscribedResourceNamesChannel.Reader.ReadAsync().DefaultTimeout();
var subscribedResourceName1 = await subscribedResourceNamesChannel.Reader.ReadAsync(TestContext.Current.CancellationToken).DefaultTimeout();
Assert.Equal("test-resource", subscribedResourceName1);

navigationManager.LocationChanged += (sender, e) =>
Expand All @@ -117,11 +118,11 @@ public async Task ResourceName_SubscribeOnLoadAndChange_SubscribeConsoleLogsOnce
cut.WaitForState(() => instance.PageViewModel.SelectedResource == testResource2);
cut.WaitForState(() => instance.PageViewModel.Status == loc[nameof(Resources.ConsoleLogs.ConsoleLogsWatchingLogs)]);

var subscribedResourceName2 = await subscribedResourceNamesChannel.Reader.ReadAsync().DefaultTimeout();
var subscribedResourceName2 = await subscribedResourceNamesChannel.Reader.ReadAsync(TestContext.Current.CancellationToken).DefaultTimeout();
Assert.Equal("test-resource2", subscribedResourceName2);

subscribedResourceNamesChannel.Writer.Complete();
Assert.False(await subscribedResourceNamesChannel.Reader.WaitToReadAsync().DefaultTimeout());
Assert.False(await subscribedResourceNamesChannel.Reader.WaitToReadAsync(TestContext.Current.CancellationToken).DefaultTimeout());
}

[Fact]
Expand Down
9 changes: 8 additions & 1 deletion tests/Aspire.Dashboard.Tests/Aspire.Dashboard.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
<!--
CS8002: Referenced assembly does not have a strong name
-->
<NoWarn>$(NoWarn);CS8002</NoWarn>
<!--
FIXME: https://github.com/dotnet/aspire/issues/8488
xUnit1051: Calls to methods which accept CancellationToken should use TestContext.Current.CancellationToken to allow test cancellation to be more responsive

This is being disabled here in addition to any central location, because this project
gets built independently on helix.
-->
<NoWarn>$(NoWarn);CS8002;xUnit1051</NoWarn>

<InstallBrowsersForPlaywright Condition="'$(InstallBrowsersForPlaywright)' == '' and '$(CODESPACES)' == 'true'">true</InstallBrowsersForPlaywright>
<InstallBrowsersForPlaywright Condition="'$(InstallBrowsersForPlaywright)' == '' and '$(ContinuousIntegrationBuild)' == 'true'">true</InstallBrowsersForPlaywright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public async Task AddElasticsearchClient_HealthCheckShouldBeRegisteredWhenEnable

var healthCheckService = host.Services.GetRequiredService<HealthCheckService>();

var healthCheckReport = await healthCheckService.CheckHealthAsync();
var healthCheckReport = await healthCheckService.CheckHealthAsync(TestContext.Current.CancellationToken);

var healthCheckName = useKeyed ? $"Elastic.Clients.Elasticsearch_{key}" : "Elastic.Clients.Elasticsearch";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void AddMultiple(this IDistributedApplicationBuilder builder, [Res
CompilerError(diagnostic.Id).WithLocation(8, 5).WithMessage(message2)
]);

await test.RunAsync();
await test.RunAsync(TestContext.Current.CancellationToken);
}

[Theory]
Expand Down Expand Up @@ -74,6 +74,6 @@ public static void AddMultiple(this IDistributedApplicationBuilder builder, [Res
CompilerError(diagnostic.Id).WithLocation(6, 21).WithMessage(message2)
]);

await test.RunAsync();
await test.RunAsync(TestContext.Current.CancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task EndpointNameInvalid(string endpointName)
""",
[CompilerError(diagnostic.Id).WithLocation(6, 37).WithMessage(message)]);

await test.RunAsync();
await test.RunAsync(TestContext.Current.CancellationToken);
}

[Theory]
Expand Down Expand Up @@ -66,7 +66,7 @@ public static IResourceBuilder<ContainerResource> WithEndpoints(
CompilerError(diagnostic.Id).WithLocation(9, 9).WithMessage(message2)
]);

await test.RunAsync();
await test.RunAsync(TestContext.Current.CancellationToken);
}

[Theory]
Expand All @@ -82,7 +82,7 @@ public async Task EndpointNameValid(string endpointName)
.WithEndpoint(port: 1234, name: "{{endpointName}}");
""", []);

await test.RunAsync();
await test.RunAsync(TestContext.Current.CancellationToken);
}

[Theory]
Expand Down Expand Up @@ -112,6 +112,6 @@ public static IResourceBuilder<ContainerResource> WithEndpoints(
}
""", []);

await test.RunAsync();
await test.RunAsync(TestContext.Current.CancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async Task ResourceNameInvalid(string resourceName)
""",
[CompilerError(diagnostic.Id).WithLocation(5, 22).WithMessage(message)]);

await test.RunAsync();
await test.RunAsync(TestContext.Current.CancellationToken);
}

[Theory]
Expand Down Expand Up @@ -61,7 +61,7 @@ public static void AddMultipleParameters(this IDistributedApplicationBuilder bui
CompilerError(diagnostic.Id).WithLocation(8, 5).WithMessage(message2)
]);

await test.RunAsync();
await test.RunAsync(TestContext.Current.CancellationToken);
}

[Theory]
Expand All @@ -76,7 +76,7 @@ public async Task ResourceNameValid(string resourceName)
builder.AddParameter("{{resourceName}}");
""", []);

await test.RunAsync();
await test.RunAsync(TestContext.Current.CancellationToken);
}

[Theory]
Expand All @@ -102,6 +102,6 @@ public static void AddMultipleParameters(this IDistributedApplicationBuilder bui
}
""", []);

await test.RunAsync();
await test.RunAsync(TestContext.Current.CancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>$(DefaultTargetFramework)</TargetFramework>
<NoWarn>$(NoWarn);xUnit1051</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>$(DefaultTargetFramework)</TargetFramework>
<NoWarn>$(NoWarn);xUnit1051</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>$(DefaultTargetFramework)</TargetFramework>
<NoWarn>$(NoWarn);ASPIREHOSTINGPYTHON001;</NoWarn>
<NoWarn>$(NoWarn);ASPIREHOSTINGPYTHON001;xUnit1051</NoWarn>

<!--
Do not run tests in Helix at all
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>$(DefaultTargetFramework)</TargetFramework>
<NoWarn>$(NoWarn);xUnit1051</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>$(DefaultTargetFramework)</TargetFramework>
<NoWarn>$(NoWarn);xUnit1051</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading
Loading