-
Notifications
You must be signed in to change notification settings - Fork 2
/
DisposableTests.cs
94 lines (90 loc) · 3.36 KB
/
DisposableTests.cs
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
84
85
86
87
88
89
90
91
92
93
94
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 path = GetDisposalObjectTestProjectFilePath();
var logPath = Path.GetTempFileName();
var processStartInfo = new ProcessStartInfo
{
FileName = "dotnet",
Arguments = $"run -p \"{path}\" -f netcoreapp2.1 \"{logPath}\" false",
UseShellExecute = false,
//RedirectStandardOutput = true,
CreateNoWindow = true
};
using (var process = Process.Start(processStartInfo))
{
//string line = process.StandardOutput.ReadToEnd();
process.WaitForExit();
}
var result = File.ReadAllText(logPath);
Assert.Equal("21", result);
File.Delete(logPath);
}
[Fact]
public static void DisposalAtProcessKillTest()
{
var path = GetDisposalObjectTestProjectFilePath();
var logPath = Path.GetTempFileName();
var processStartInfo = new ProcessStartInfo
{
FileName = "dotnet",
Arguments = $"run -p \"{path}\" -f netcoreapp2.1 \"{logPath}\" true",
UseShellExecute = false,
//RedirectStandardOutput = true,
CreateNoWindow = true
};
using (var process = Process.Start(processStartInfo))
{
//string line = process.StandardOutput.ReadToEnd();
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 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 NET471
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;
}
}
}