1
- // Copyright (c) .NET Foundation. All rights reserved.
1
+ // Copyright (c) .NET Foundation. All rights reserved.
2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
4
using System ;
5
5
using System . Collections . Generic ;
6
6
using System . IO ;
7
7
using System . Runtime . InteropServices ;
8
8
using System . Threading ;
9
+ using System . Threading . Tasks ;
9
10
using Microsoft . AspNetCore . Razor . Tools ;
10
11
using Microsoft . CodeAnalysis ;
11
12
using Moq ;
13
+ using Xunit ;
12
14
13
15
namespace Microsoft . AspNetCore . Razor . Design . IntegrationTests
14
16
{
15
- public abstract class BuildServerTestFixtureBase : IDisposable
17
+ public abstract class BuildServerTestFixtureBase : IAsyncLifetime
16
18
{
17
19
private static readonly TimeSpan _defaultShutdownTimeout = TimeSpan . FromSeconds ( 120 ) ;
18
20
@@ -23,19 +25,18 @@ protected BuildServerTestFixtureBase(string pipeName)
23
25
24
26
public string PipeName { get ; }
25
27
26
- public void Dispose ( )
28
+ public Task InitializeAsync ( )
29
+ {
30
+ return Task . CompletedTask ;
31
+ }
32
+
33
+ public async Task DisposeAsync ( )
27
34
{
28
35
// Shutdown the build server.
29
36
using ( var cts = new CancellationTokenSource ( _defaultShutdownTimeout ) )
30
37
{
31
38
var writer = new StringWriter ( ) ;
32
39
33
- cts . Token . Register ( ( ) =>
34
- {
35
- var output = writer . ToString ( ) ;
36
- throw new TimeoutException ( $ "Shutting down the build server at pipe { PipeName } took longer than expected.{ Environment . NewLine } Output: { output } .") ;
37
- } ) ;
38
-
39
40
var application = new Application ( cts . Token , Mock . Of < ExtensionAssemblyLoader > ( ) , Mock . Of < ExtensionDependencyChecker > ( ) , ( path , properties ) => Mock . Of < PortableExecutableReference > ( ) , writer , writer ) ;
40
41
41
42
var args = new List < string >
@@ -52,7 +53,21 @@ public void Dispose()
52
53
args . Add ( "-w" ) ;
53
54
}
54
55
55
- var exitCode = application . Execute ( args . ToArray ( ) ) ;
56
+ var executeTask = Task . Run ( ( ) => application . Execute ( args . ToArray ( ) ) ) ;
57
+
58
+ if ( executeTask == await Task . WhenAny ( executeTask , Task . Delay ( Timeout . Infinite , cts . Token ) ) )
59
+ {
60
+ // Complete the Task.Delay
61
+ cts . Cancel ( ) ;
62
+ }
63
+ else
64
+ {
65
+ var output = writer . ToString ( ) ;
66
+ throw new TimeoutException ( $ "Shutting down the build server at pipe { PipeName } took longer than expected.{ Environment . NewLine } Output: { output } .") ;
67
+ }
68
+
69
+ var exitCode = await executeTask ;
70
+
56
71
if ( exitCode != 0 )
57
72
{
58
73
var output = writer . ToString ( ) ;
0 commit comments