-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathAbstractIdeIntegrationTest.cs
More file actions
116 lines (96 loc) · 3.66 KB
/
Copy pathAbstractIdeIntegrationTest.cs
File metadata and controls
116 lines (96 loc) · 3.66 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using Microsoft.CodeAnalysis.Testing.InProcess;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Threading;
using Xunit;
using Task = System.Threading.Tasks.Task;
namespace Microsoft.CodeAnalysis.Testing
{
[IdeSettings(MinVersion = VisualStudioVersion.VS2022)]
public abstract class AbstractIdeIntegrationTest : IAsyncLifetime, IDisposable
{
/// <summary>
/// A long timeout used to avoid hangs in tests, where a test failure manifests as an operation never occurring.
/// </summary>
public static readonly TimeSpan HangMitigatingTimeout = TimeSpan.FromMinutes(4);
private JoinableTaskContext? _joinableTaskContext;
private JoinableTaskCollection? _joinableTaskCollection;
private JoinableTaskFactory? _joinableTaskFactory;
private TestServices? _testServices;
private CancellationTokenSource _hangMitigatingCancellationTokenSource;
protected AbstractIdeIntegrationTest()
{
Assert.True(Application.Current.Dispatcher.CheckAccess());
JoinableTaskContext = ThreadHelper.JoinableTaskContext;
_hangMitigatingCancellationTokenSource = new CancellationTokenSource(HangMitigatingTimeout);
}
[NotNull]
protected JoinableTaskContext? JoinableTaskContext
{
get
{
return _joinableTaskContext ?? throw new InvalidOperationException();
}
private set
{
if (value == _joinableTaskContext)
{
return;
}
if (value is null)
{
_joinableTaskContext = null;
_joinableTaskCollection = null;
_joinableTaskFactory = null;
}
else
{
_joinableTaskContext = value;
_joinableTaskCollection = value.CreateCollection();
_joinableTaskFactory = value.CreateFactory(_joinableTaskCollection).WithPriority(Application.Current.Dispatcher, DispatcherPriority.Background);
}
}
}
[NotNull]
protected TestServices? TestServices
{
get
{
return _testServices ?? throw new InvalidOperationException();
}
private set
{
_testServices = value;
}
}
protected JoinableTaskFactory JoinableTaskFactory
=> _joinableTaskFactory ?? throw new InvalidOperationException();
protected CancellationToken HangMitigatingCancellationToken
=> _hangMitigatingCancellationTokenSource.Token;
public virtual async Task InitializeAsync()
{
TestServices = await CreateTestServicesAsync();
}
public virtual async Task DisposeAsync()
{
if (_joinableTaskCollection is object)
{
await _joinableTaskCollection.JoinTillEmptyAsync();
}
JoinableTaskContext = null;
}
public virtual void Dispose()
{
}
protected virtual async Task<TestServices> CreateTestServicesAsync()
=> await TestServices.CreateAsync(JoinableTaskFactory);
}
}