未验证 提交 906d5cb3 编写于 作者: A Ashley Hauck 提交者: GitHub

Merge pull request #23518 from khyperia/flaky_server_test

Fix flaky compiler server test
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.IO;
using System.Runtime.CompilerServices;
......@@ -9,8 +9,9 @@ namespace Microsoft.CodeAnalysis.Test.Utilities
{
public sealed class TempRoot : IDisposable
{
private readonly List<IDisposable> _temps = new List<IDisposable>();
private readonly ConcurrentBag<IDisposable> _temps = new ConcurrentBag<IDisposable>();
public static readonly string Root;
private bool _disposed;
static TempRoot()
{
......@@ -20,16 +21,8 @@ static TempRoot()
public void Dispose()
{
if (_temps != null)
{
DisposeAll(_temps);
_temps.Clear();
}
}
private static void DisposeAll(IEnumerable<IDisposable> temps)
{
foreach (var temp in temps)
_disposed = true;
while (_temps.TryTake(out var temp))
{
try
{
......@@ -45,8 +38,17 @@ private static void DisposeAll(IEnumerable<IDisposable> temps)
}
}
private void CheckDisposed()
{
if (this._disposed)
{
throw new ObjectDisposedException(nameof(TempRoot));
}
}
public TempDirectory CreateDirectory()
{
CheckDisposed();
var dir = new DisposableDirectory(this);
_temps.Add(dir);
return dir;
......@@ -54,11 +56,13 @@ public TempDirectory CreateDirectory()
public TempFile CreateFile(string prefix = null, string extension = null, string directory = null, [CallerFilePath]string callerSourcePath = null, [CallerLineNumber]int callerLineNumber = 0)
{
CheckDisposed();
return AddFile(new DisposableFile(prefix, extension, directory, callerSourcePath, callerLineNumber));
}
public DisposableFile AddFile(DisposableFile file)
{
CheckDisposed();
_temps.Add(file);
return file;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册