Skip to content
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
7 changes: 4 additions & 3 deletions test/FunctionalTests/Linker/Helpers/DotNetProcess.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#region Copyright notice and license
#region Copyright notice and license

// Copyright 2019 The gRPC Authors
//
Expand Down Expand Up @@ -54,9 +54,10 @@ public DotNetProcess()
public int ExitCode => Process.ExitCode;
public bool HasExited => Process.HasExited;

public void Start(string arguments)
public void Start(string fileName, string? arguments)
{
Process.StartInfo.Arguments = arguments;
Process.StartInfo.FileName = fileName;
Process.StartInfo.Arguments = arguments ?? string.Empty;
Process.Start();

Process.BeginOutputReadLine();
Expand Down
41 changes: 32 additions & 9 deletions test/FunctionalTests/Linker/LinkerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,21 @@ public class LinkerTests
{
private static readonly TimeSpan Timeout = TimeSpan.FromSeconds(120);

#if NET8_0_OR_GREATER
[Test]
public async Task RunWebsiteAndCallWithClient_Success()
public async Task RunWebsiteAndCallWithClient_Aot_Success()
{
await RunWebsiteAndCallWithClient(publishAot: true);
}
#endif

[Test]
public async Task RunWebsiteAndCallWithClient_Trimming_Success()
{
await RunWebsiteAndCallWithClient(publishAot: false);
}

private async Task RunWebsiteAndCallWithClient(bool publishAot)
{
var projectDirectory = typeof(LinkerTests).Assembly
.GetCustomAttributes<AssemblyMetadataAttribute>()
Expand All @@ -51,13 +64,11 @@ public async Task RunWebsiteAndCallWithClient_Success()
try
{
using var cts = new CancellationTokenSource();
using var websiteProcess = new WebsiteProcess();
using var clientProcess = new DotNetProcess();

try
{
var publishWebsiteTask = PublishAppAsync(projectDirectory + @"\..\..\testassets\LinkerTestsWebsite\LinkerTestsWebsite.csproj", linkerTestsWebsitePath, cts.Token);
var publishClientTask = PublishAppAsync(projectDirectory + @"\..\..\testassets\LinkerTestsClient\LinkerTestsClient.csproj", linkerTestsClientPath, cts.Token);
var publishWebsiteTask = PublishAppAsync(projectDirectory + @"\..\..\testassets\LinkerTestsWebsite\LinkerTestsWebsite.csproj", linkerTestsWebsitePath, publishAot, cts.Token);
var publishClientTask = PublishAppAsync(projectDirectory + @"\..\..\testassets\LinkerTestsClient\LinkerTestsClient.csproj", linkerTestsClientPath, publishAot, cts.Token);

await Task.WhenAll(publishWebsiteTask, publishClientTask).TimeoutAfter(Timeout);
Console.WriteLine("Successfully published app.");
Expand All @@ -67,12 +78,15 @@ public async Task RunWebsiteAndCallWithClient_Success()
cts.Dispose();
}

using var websiteProcess = new WebsiteProcess();
using var clientProcess = new DotNetProcess();

try
{
websiteProcess.Start(Path.Combine(linkerTestsWebsitePath, "LinkerTestsWebsite.dll"));
websiteProcess.Start(BuildStartPath(linkerTestsWebsitePath, "LinkerTestsWebsite"), arguments: null);
await websiteProcess.WaitForReadyAsync().TimeoutAfter(Timeout);

clientProcess.Start(Path.Combine(linkerTestsClientPath, $"LinkerTestsClient.dll {websiteProcess.ServerPort}"));
clientProcess.Start(BuildStartPath(linkerTestsClientPath, "LinkerTestsClient"), arguments: websiteProcess.ServerPort!.ToString());
await clientProcess.WaitForExitAsync().TimeoutAfter(Timeout);
}
finally
Expand All @@ -92,6 +106,13 @@ public async Task RunWebsiteAndCallWithClient_Success()
}
}

private static string BuildStartPath(string path, string projectName)
{
// Executable on Windows has an *.exe extension.
// We don't need to add it to the start path because *.exe is in the PATHEXT env var.
return Path.Combine(path, projectName);
}

private static void EnsureDeleted(string path)
{
if (Directory.Exists(path))
Expand All @@ -100,7 +121,7 @@ private static void EnsureDeleted(string path)
}
}

private static async Task PublishAppAsync(string path, string outputPath, CancellationToken cancellationToken)
private static async Task PublishAppAsync(string path, string outputPath, bool publishAot, CancellationToken cancellationToken)
{
var resolvedPath = Path.GetFullPath(path);
Console.WriteLine($"Publishing {resolvedPath}");
Expand All @@ -110,7 +131,9 @@ private static async Task PublishAppAsync(string path, string outputPath, Cancel

try
{
process.Start($"publish {resolvedPath} -r {GetRuntimeIdentifier()} -c Release -o {outputPath} --self-contained");
// The AppPublishAot parameter is used to tell the compiler to publish as AOT.
// AppPublishAot is used instead of PublishAot because dependency projects have non-AOT targets. Setting "PublishAot=true" causes build errors.
process.Start("dotnet", $"publish {resolvedPath} -r {GetRuntimeIdentifier()} -c Release -o {outputPath} -p:AppPublishAot={publishAot} --self-contained");
await process.WaitForExitAsync().TimeoutAfter(Timeout);
}
catch (Exception ex)
Expand Down
1 change: 1 addition & 0 deletions testassets/LinkerTestsClient/LinkerTestsClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<OutputType>Exe</OutputType>
<PublishTrimmed>true</PublishTrimmed>
<PublishAot>$(AppPublishAot)</PublishAot>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions testassets/LinkerTestsWebsite/LinkerTestsWebsite.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<OutputType>Exe</OutputType>
<PublishTrimmed>true</PublishTrimmed>
<PublishAot>$(AppPublishAot)</PublishAot>
<!--
ASP.NET Core SDK has partial trimming in .NET 7. Change to full because gRPC works with full
and it removes linker warnings from unused ASP.NET Core assemblies.
Expand Down