Skip to content

Commit 59bfc47

Browse files
authored
Merge pull request microsoft#1959 from tyrielv/tyrielv/fix-flaky-writewithoutclose
Fix flaky WriteWithoutClose/CreateFileWithoutClose functional tests
2 parents 50b24e6 + 1731fca commit 59bfc47

7 files changed

Lines changed: 62 additions & 31 deletions

File tree

GVFS/GVFS.FunctionalTests/FileSystemRunners/BashRunner.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,12 @@ public override long FileSize(string path)
304304
return long.Parse(this.RunProcess(statCommand));
305305
}
306306

307-
public override void CreateFileWithoutClose(string path)
307+
public override IDisposable CreateFileWithoutClose(string path)
308308
{
309309
throw new NotImplementedException();
310310
}
311311

312-
public override void OpenFileAndWriteWithoutClose(string path, string data)
312+
public override IDisposable OpenFileAndWriteWithoutClose(string path, string data)
313313
{
314314
throw new NotImplementedException();
315315
}

GVFS/GVFS.FunctionalTests/FileSystemRunners/CmdRunner.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,12 @@ public override void ChangeMode(string path, ushort mode)
238238
throw new NotSupportedException();
239239
}
240240

241-
public override void CreateFileWithoutClose(string path)
242-
{
241+
public override IDisposable CreateFileWithoutClose(string path)
242+
{
243243
throw new NotImplementedException();
244-
}
245-
246-
public override void OpenFileAndWriteWithoutClose(string path, string data)
244+
}
245+
246+
public override IDisposable OpenFileAndWriteWithoutClose(string path, string data)
247247
{
248248
throw new NotImplementedException();
249249
}

GVFS/GVFS.FunctionalTests/FileSystemRunners/FileSystemRunner.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ public static FileSystemRunner DefaultRunner
7676
/// <param name="path">Path to file</param>
7777
/// <param name="contents">File contents</param>
7878
public abstract void WriteAllText(string path, string contents);
79-
public abstract void CreateFileWithoutClose(string path);
80-
public abstract void OpenFileAndWriteWithoutClose(string path, string data);
79+
public abstract IDisposable CreateFileWithoutClose(string path);
80+
public abstract IDisposable OpenFileAndWriteWithoutClose(string path, string data);
8181

8282
/// <summary>
8383
/// Append the specified contents to the specified file. By calling this method the caller is

GVFS/GVFS.FunctionalTests/FileSystemRunners/PowerShellRunner.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using GVFS.Tests.Should;
2+
using System;
23
using System.IO;
34

45
namespace GVFS.FunctionalTests.FileSystemRunners
@@ -217,12 +218,12 @@ public override void ChangeMode(string path, ushort mode)
217218
throw new System.NotSupportedException();
218219
}
219220

220-
public override void CreateFileWithoutClose(string path)
221+
public override IDisposable CreateFileWithoutClose(string path)
221222
{
222223
throw new System.NotSupportedException();
223-
}
224-
225-
public override void OpenFileAndWriteWithoutClose(string path, string data)
224+
}
225+
226+
public override IDisposable OpenFileAndWriteWithoutClose(string path, string data)
226227
{
227228
throw new System.NotSupportedException();
228229
}

GVFS/GVFS.FunctionalTests/FileSystemRunners/SystemIORunner.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ public override string MoveFile(string sourcePath, string targetPath)
2222
return string.Empty;
2323
}
2424

25-
public override void CreateFileWithoutClose(string path)
25+
public override IDisposable CreateFileWithoutClose(string path)
2626
{
27-
File.Create(path);
28-
}
29-
30-
public override void OpenFileAndWriteWithoutClose(string path, string content)
27+
return File.Create(path);
28+
}
29+
30+
public override IDisposable OpenFileAndWriteWithoutClose(string path, string content)
3131
{
3232
StreamWriter file = new StreamWriter(path);
3333
file.Write(content);
34+
file.Flush();
35+
return file;
3436
}
3537

3638
public override void MoveFileShouldFail(string sourcePath, string targetPath)

GVFS/GVFS.FunctionalTests/Tests/GitCommands/GitRepoTests.cs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -326,22 +326,24 @@ protected void CreateFile(string content, params string[] filePathPaths)
326326
this.FileSystem.WriteAllText(controlFile, content);
327327
}
328328

329-
protected void CreateFileWithoutClose(string path)
330-
{
329+
protected IDisposable CreateFileWithoutClose(string path)
330+
{
331331
string virtualFile = Path.Combine(this.Enlistment.RepoRoot, path);
332332
string controlFile = Path.Combine(this.ControlGitRepo.RootPath, path);
333-
this.FileSystem.CreateFileWithoutClose(virtualFile);
334-
this.FileSystem.CreateFileWithoutClose(controlFile);
335-
}
336-
337-
protected void ReadFileAndWriteWithoutClose(string path, string contents)
333+
IDisposable virtualHandle = this.FileSystem.CreateFileWithoutClose(virtualFile);
334+
IDisposable controlHandle = this.FileSystem.CreateFileWithoutClose(controlFile);
335+
return new CompositeDisposable(virtualHandle, controlHandle);
336+
}
337+
338+
protected IDisposable ReadFileAndWriteWithoutClose(string path, string contents)
338339
{
339340
string virtualFile = Path.Combine(this.Enlistment.RepoRoot, path);
340341
string controlFile = Path.Combine(this.ControlGitRepo.RootPath, path);
341342
this.FileSystem.ReadAllText(virtualFile);
342343
this.FileSystem.ReadAllText(controlFile);
343-
this.FileSystem.OpenFileAndWriteWithoutClose(virtualFile, contents);
344-
this.FileSystem.OpenFileAndWriteWithoutClose(controlFile, contents);
344+
IDisposable virtualHandle = this.FileSystem.OpenFileAndWriteWithoutClose(virtualFile, contents);
345+
IDisposable controlHandle = this.FileSystem.OpenFileAndWriteWithoutClose(controlFile, contents);
346+
return new CompositeDisposable(virtualHandle, controlHandle);
345347
}
346348

347349
protected void CreateFolder(string folderPath)
@@ -667,5 +669,27 @@ protected void FilesShouldMatchAfterConflict()
667669
this.FileContentsShouldMatch("Test_ConflictTests", "ModifiedFiles", "SameChange.txt");
668670
this.FileContentsShouldMatch("Test_ConflictTests", "ModifiedFiles", "SuccessfulMerge.txt");
669671
}
672+
673+
/// <summary>
674+
/// Disposes multiple <see cref="IDisposable"/> objects as a single unit.
675+
/// Used to hold file handles open for the duration of a test scope.
676+
/// </summary>
677+
protected sealed class CompositeDisposable : IDisposable
678+
{
679+
private readonly IDisposable[] disposables;
680+
681+
public CompositeDisposable(params IDisposable[] disposables)
682+
{
683+
this.disposables = disposables;
684+
}
685+
686+
public void Dispose()
687+
{
688+
foreach (IDisposable disposable in this.disposables)
689+
{
690+
disposable?.Dispose();
691+
}
692+
}
693+
}
670694
}
671695
}

GVFS/GVFS.FunctionalTests/Tests/GitCommands/StatusTests.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,20 @@ public void DeleteThenCreateThenDeleteFile()
4646
public void CreateFileWithoutClose()
4747
{
4848
string srcPath = @"CreateFileWithoutClose.md";
49-
this.CreateFileWithoutClose(srcPath);
50-
this.ValidGitStatusWithRetry(srcPath);
49+
using (IDisposable handles = this.CreateFileWithoutClose(srcPath))
50+
{
51+
this.ValidGitStatusWithRetry(srcPath);
52+
}
5153
}
5254

5355
[TestCase]
5456
public void WriteWithoutClose()
5557
{
5658
string srcPath = @"Readme.md";
57-
this.ReadFileAndWriteWithoutClose(srcPath, "More Stuff");
58-
this.ValidGitStatusWithRetry(srcPath);
59+
using (IDisposable handles = this.ReadFileAndWriteWithoutClose(srcPath, "More Stuff"))
60+
{
61+
this.ValidGitStatusWithRetry(srcPath);
62+
}
5963
}
6064

6165
[TestCase]

0 commit comments

Comments
 (0)