Skip to content

Commit bdb5923

Browse files
committed
Fix temp data tests on 4.8
1 parent 890f90e commit bdb5923

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

CodeJam.Main.Tests/IO/TempDataTests.cs

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ private static void AssertDisposed<T>(Func<T> memberCallback) =>
1414
Assert.Throws<ObjectDisposedException>(() => memberCallback());
1515
#endregion
1616

17+
[MethodImpl(MethodImplOptions.NoInlining)]
18+
private static string CreateAndLeakTempDir(string s)
19+
{
20+
var dir2 = TempData.CreateDirectory();
21+
var dir2Path = dir2.Path;
22+
Assert.AreNotEqual(s, dir2Path, "Path should not match");
23+
Assert.IsNotNull(dir2.Info, "Info is null");
24+
Assert.IsTrue(dir2.Info.Exists, "Directory should exist");
25+
GC.KeepAlive(dir2);
26+
return dir2Path;
27+
}
28+
1729
[Test]
1830
public void Test01Directory()
1931
{
@@ -35,16 +47,7 @@ public void Test01Directory()
3547

3648
// test for cleanup if leaked
3749
{
38-
var dir2 = TempData.CreateDirectory();
39-
var dir2Path = dir2.Path;
40-
Assert.AreNotEqual(dirPath, dir2Path, "Path should not match");
41-
Assert.IsNotNull(dir2.Info, "Info is null");
42-
Assert.IsTrue(dir2.Info.Exists, "Directory should exist");
43-
GC.KeepAlive(dir2);
44-
45-
// clear GC root for a debug build
46-
// ReSharper disable once RedundantAssignment
47-
dir2 = null;
50+
var dir2Path = CreateAndLeakTempDir(dirPath);
4851
GC.Collect();
4952
GC.WaitForPendingFinalizers();
5053
GC.Collect();
@@ -118,6 +121,19 @@ public void Test03DirectorySpecificPath()
118121
Assert.IsFalse(File.Exists(dirPath), "Directory should NOT exist");
119122
}
120123

124+
[MethodImpl(MethodImplOptions.NoInlining)]
125+
private static string CreateAndLeakTempFile(string filePath)
126+
{
127+
var file2 = TempData.CreateFile();
128+
var file2Path = file2.Path;
129+
Assert.AreNotEqual(filePath, file2Path, "Path should not match");
130+
Assert.IsNotNull(file2.Info, "Info is null");
131+
Assert.IsTrue(file2.Info.Exists, "File should exist");
132+
GC.KeepAlive(file2);
133+
134+
return file2Path;
135+
}
136+
121137
[Test]
122138
public void Test04File()
123139
{
@@ -139,19 +155,11 @@ public void Test04File()
139155

140156
// test for cleanup if leaked
141157
{
142-
var file2 = TempData.CreateFile();
143-
var file2Path = file2.Path;
144-
Assert.AreNotEqual(filePath, file2Path, "Path should not match");
145-
Assert.IsNotNull(file2.Info, "Info is null");
146-
Assert.IsTrue(file2.Info.Exists, "File should exist");
147-
GC.KeepAlive(file2);
148-
149-
// clear GC root for a debug build
150-
// ReSharper disable once RedundantAssignment
151-
file2 = null;
158+
var file2Path = CreateAndLeakTempFile(filePath);
152159
GC.Collect();
153160
GC.WaitForPendingFinalizers();
154161
GC.Collect();
162+
155163
Assert.IsFalse(File.Exists(file2Path), "File should NOT exist");
156164
}
157165

@@ -217,6 +225,18 @@ public void Test06FileSpecificPath()
217225
Assert.IsFalse(File.Exists(filePath), "File should NOT exist");
218226
}
219227

228+
[MethodImpl(MethodImplOptions.NoInlining)]
229+
private static string CreateAndLeakTempStream(string filePath)
230+
{
231+
var file2 = TempData.CreateFileStream();
232+
var file2Path = file2.Name;
233+
Assert.AreNotEqual(filePath, file2Path, "Path should not match");
234+
Assert.IsTrue(File.Exists(file2.Name), "FileStream should exist");
235+
GC.KeepAlive(file2);
236+
237+
return file2Path;
238+
}
239+
220240
[Test]
221241
public void Test07FileStream()
222242
{
@@ -238,15 +258,7 @@ public void Test07FileStream()
238258

239259
// test for cleanup if leaked
240260
{
241-
var file2 = TempData.CreateFileStream();
242-
var file2Path = file2.Name;
243-
Assert.AreNotEqual(filePath, file2Path, "Path should not match");
244-
Assert.IsTrue(File.Exists(file2.Name), "FileStream should exist");
245-
GC.KeepAlive(file2);
246-
247-
// clear GC root for a debug build
248-
// ReSharper disable once RedundantAssignment
249-
file2 = null;
261+
var file2Path = CreateAndLeakTempStream(filePath);
250262
GC.Collect();
251263
GC.WaitForPendingFinalizers();
252264
GC.Collect();

0 commit comments

Comments
 (0)