Add targeted local port cleanup to desktop workbench
This commit is contained in:
1 parent
51c4f86918
commit
ab9cc8d0a5
8 files changed
+312
-1
No files matched your search
@@ -9,5 +9,6 @@
|
||||
<PackageReference Include="xunit" Version="2.9.3"/>
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2"><PrivateAssets>all</PrivateAssets></PackageReference>
|
||||
<ProjectReference Include="../Ads.Desktop/Ads.Desktop.csproj"/>
|
||||
<ProjectReference Include="../Ads.Desktop.Smoke/Ads.Desktop.Smoke.csproj" ReferenceOutputAssembly="false"/>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,86 @@
|
||||
using System.Diagnostics;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using Ads.Desktop;
|
||||
using Xunit;
|
||||
|
||||
namespace Ads.Desktop.Tests;
|
||||
|
||||
public class PortCleanupTests
|
||||
{
|
||||
private static int FreePort()
|
||||
{
|
||||
var listener = new TcpListener(IPAddress.Loopback, 0);
|
||||
listener.Start();
|
||||
try { return ((IPEndPoint)listener.LocalEndpoint).Port; }
|
||||
finally { listener.Stop(); }
|
||||
}
|
||||
|
||||
private static async Task<Process> ChildAsync(int port)
|
||||
{
|
||||
var configuration = new DirectoryInfo(AppContext.BaseDirectory).Parent!.Name;
|
||||
var assembly = Path.Combine(DesktopPaths.ProjectRoot()!, "desktop", "Ads.Desktop.Smoke", "bin", configuration, "net8.0", "Ads.Desktop.Smoke.dll");
|
||||
var info = ProcessTools.CreateStartInfo("dotnet", [assembly, "--port-child", port.ToString()]);
|
||||
var child = Process.Start(info)!;
|
||||
try
|
||||
{
|
||||
var ready = await child.StandardOutput.ReadLineAsync().WaitAsync(TimeSpan.FromSeconds(10));
|
||||
Assert.Equal("READY", ready);
|
||||
return child;
|
||||
}
|
||||
catch { Stop(child); throw; }
|
||||
}
|
||||
|
||||
private static void Stop(Process process)
|
||||
{
|
||||
if (!process.HasExited) { process.Kill(); process.WaitForExit(5000); }
|
||||
process.Dispose();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CleanupTerminatesOnlyTheListenerAndReleasesItsPort()
|
||||
{
|
||||
if (!OperatingSystem.IsWindows()) return;
|
||||
var targetPort = FreePort();
|
||||
var otherPort = FreePort();
|
||||
while (otherPort == targetPort) otherPort = FreePort();
|
||||
var target = await ChildAsync(targetPort);
|
||||
Process? unrelated = null;
|
||||
try
|
||||
{
|
||||
unrelated = await ChildAsync(otherPort);
|
||||
var owners = LocalPortProcesses.Inspect(targetPort);
|
||||
Assert.Single(owners);
|
||||
Assert.Equal(target.Id, owners[0].Id);
|
||||
Assert.Equal(1, await LocalPortProcesses.TerminateAsync(targetPort, owners));
|
||||
Assert.True(target.HasExited);
|
||||
Assert.False(unrelated.HasExited);
|
||||
Assert.Empty(LocalPortProcesses.Inspect(targetPort));
|
||||
var probe = new TcpListener(IPAddress.Loopback, targetPort);
|
||||
probe.Start(); probe.Stop();
|
||||
}
|
||||
finally { Stop(target); if (unrelated != null) Stop(unrelated); }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ChangedProcessIdentityIsNotTerminated()
|
||||
{
|
||||
if (!OperatingSystem.IsWindows()) return;
|
||||
var port = FreePort();
|
||||
var child = await ChildAsync(port);
|
||||
try
|
||||
{
|
||||
var owner = Assert.Single(LocalPortProcesses.Inspect(port));
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(() => LocalPortProcesses.TerminateAsync(port, [owner with { StartedUtcTicks = owner.StartedUtcTicks + 1 }]));
|
||||
Assert.False(child.HasExited);
|
||||
}
|
||||
finally { Stop(child); }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DoesNotTerminateCurrentWorkbenchOrAdbMainService()
|
||||
{
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(() => LocalPortProcesses.TerminateAsync(5037, []));
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(() => LocalPortProcesses.TerminateAsync(15555, [new PortProcess(Environment.ProcessId, "self", 0)]));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user