-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDisposableTests.cs
More file actions
83 lines (81 loc) · 3 KB
/
DisposableTests.cs
File metadata and controls
83 lines (81 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using Xunit;
namespace Platform.Disposables.Tests
{
public static class DisposableTests
{
[Fact]
public static void DisposalOrderTest()
{
var logPath = Path.GetTempFileName();
using (var process = Process.Start(CreateProcessStartInfo(logPath, waitForCancellation: false)))
{
process.WaitForExit();
}
var result = File.ReadAllText(logPath);
Assert.Equal("21", result);
File.Delete(logPath);
}
[Fact]
public static void DisposalAtProcessKillTest()
{
var logPath = Path.GetTempFileName();
using (var process = Process.Start(CreateProcessStartInfo(logPath, waitForCancellation: true)))
{
Thread.Sleep(1000);
process.Kill();
}
var result = File.ReadAllText(logPath);
Assert.Equal("", result); // Currently, process termination will not release resources
File.Delete(logPath);
}
private static ProcessStartInfo CreateProcessStartInfo(string logPath, bool waitForCancellation)
{
var projectPath = GetDisposalObjectTestProjectFilePath();
return new ProcessStartInfo
{
FileName = "dotnet",
Arguments = $"run -p \"{projectPath}\" -f net8 \"{logPath}\" {waitForCancellation.ToString()}",
UseShellExecute = false,
CreateNoWindow = true
};
}
private static string GetDisposalObjectTestProjectFilePath()
{
const string currentProjectName = nameof(Platform) + "." + nameof(Disposables) + "." + nameof(Tests);
const string disposalOrderTestProjectName = currentProjectName + "." + nameof(DisposalOrderTest);
var currentDirectory = Environment.CurrentDirectory;
var pathParts = currentDirectory.Split(Path.DirectorySeparatorChar);
var newPathParts = new List<string>();
for (var i = 0; i < pathParts.Length; i++)
{
if (string.Equals(pathParts[i], currentProjectName))
{
newPathParts.Add(disposalOrderTestProjectName);
break;
}
else
{
newPathParts.Add(pathParts[i]);
}
}
pathParts = newPathParts.ToArray();
#if NET472
var directory = string.Join(Path.DirectorySeparatorChar.ToString(), pathParts.ToArray());
#else
var directory = Path.Combine(pathParts);
#endif
var path = Path.Combine(directory, $"{disposalOrderTestProjectName}.csproj");
if (!Path.IsPathRooted(path))
{
path = $"{Path.DirectorySeparatorChar}{path}";
}
return path;
}
}
}