1- // Copyright (c) Microsoft Corporation.
1+ // Copyright (c) Microsoft Corporation.
22// Licensed under the MIT License.
33
44using System ;
@@ -39,7 +39,7 @@ public async Task InitializeAsync()
3939 {
4040 LoggerFactory factory = new ( ) ;
4141 _psesProcess = new PsesStdioProcess ( factory , true ) ;
42- await _psesProcess . Start ( ) . ConfigureAwait ( false ) ;
42+ await _psesProcess . Start ( ) . ConfigureAwait ( true ) ;
4343
4444 TaskCompletionSource < bool > initialized = new ( ) ;
4545
@@ -90,26 +90,21 @@ public async Task InitializeAsync()
9090 // that gets completed when we receive the response to Initialize
9191 // This tells us that we are ready to send messages to PSES... but are not stuck waiting for
9292 // Initialized.
93- PsesDebugAdapterClient . Initialize ( CancellationToken . None ) . ConfigureAwait ( false ) ;
94- await initialized . Task . ConfigureAwait ( false ) ;
93+ #pragma warning disable CS4014
94+ PsesDebugAdapterClient . Initialize ( CancellationToken . None ) . ConfigureAwait ( true ) ;
95+ #pragma warning restore CS4014
96+ await initialized . Task . ConfigureAwait ( true ) ;
9597 }
9698
9799 public async Task DisposeAsync ( )
98100 {
99- try
101+ await PsesDebugAdapterClient . RequestDisconnect ( new DisconnectArguments
100102 {
101- await PsesDebugAdapterClient . RequestDisconnect ( new DisconnectArguments
102- {
103- Restart = false ,
104- TerminateDebuggee = true
105- } ) . ConfigureAwait ( false ) ;
106- await _psesProcess . Stop ( ) . ConfigureAwait ( false ) ;
107- PsesDebugAdapterClient ? . Dispose ( ) ;
108- }
109- catch ( ObjectDisposedException )
110- {
111- // Language client has a disposal bug in it
112- }
103+ Restart = false ,
104+ TerminateDebuggee = true
105+ } ) . ConfigureAwait ( true ) ;
106+ await _psesProcess . Stop ( ) . ConfigureAwait ( true ) ;
107+ PsesDebugAdapterClient ? . Dispose ( ) ;
113108 }
114109
115110 private static string NewTestFile ( string script , bool isPester = false )
@@ -147,7 +142,16 @@ private string GenerateScriptFromLoggingStatements(params string[] logStatements
147142 return builder . ToString ( ) ;
148143 }
149144
150- private static string [ ] GetLog ( ) => File . ReadLines ( s_testOutputPath ) . ToArray ( ) ;
145+ private static async Task < string [ ] > GetLog ( )
146+ {
147+ while ( ! File . Exists ( s_testOutputPath ) )
148+ {
149+ await Task . Delay ( 1000 ) . ConfigureAwait ( true ) ;
150+ }
151+ // Sleep one more time after the file exists so whatever is writing can finish.
152+ await Task . Delay ( 1000 ) . ConfigureAwait ( true ) ;
153+ return File . ReadLines ( s_testOutputPath ) . ToArray ( ) ;
154+ }
151155
152156 [ Fact ]
153157 public void CanInitializeWithCorrectServerSettings ( )
@@ -165,14 +169,12 @@ public async Task CanLaunchScriptWithNoBreakpointsAsync()
165169 {
166170 string filePath = NewTestFile ( GenerateScriptFromLoggingStatements ( "works" ) ) ;
167171
168- await PsesDebugAdapterClient . LaunchScript ( filePath , Started ) . ConfigureAwait ( false ) ;
172+ await PsesDebugAdapterClient . LaunchScript ( filePath , Started ) . ConfigureAwait ( true ) ;
169173
170- ConfigurationDoneResponse configDoneResponse = await PsesDebugAdapterClient . RequestConfigurationDone ( new ConfigurationDoneArguments ( ) ) . ConfigureAwait ( false ) ;
174+ ConfigurationDoneResponse configDoneResponse = await PsesDebugAdapterClient . RequestConfigurationDone ( new ConfigurationDoneArguments ( ) ) . ConfigureAwait ( true ) ;
171175 Assert . NotNull ( configDoneResponse ) ;
172- await Task . Delay ( 2000 ) . ConfigureAwait ( false ) ;
173-
174- string [ ] log = GetLog ( ) ;
175- Assert . Equal ( "works" , log [ 0 ] ) ;
176+ Assert . Collection ( await GetLog ( ) . ConfigureAwait ( true ) ,
177+ ( i ) => Assert . Equal ( "works" , i ) ) ;
176178 }
177179
178180 [ SkippableFact ]
@@ -188,37 +190,32 @@ public async Task CanSetBreakpointsAsync()
188190 "after breakpoint"
189191 ) ) ;
190192
191- await PsesDebugAdapterClient . LaunchScript ( filePath , Started ) . ConfigureAwait ( false ) ;
193+ await PsesDebugAdapterClient . LaunchScript ( filePath , Started ) . ConfigureAwait ( true ) ;
192194
193195 // {"command":"setBreakpoints","arguments":{"source":{"name":"dfsdfg.ps1","path":"/Users/tyleonha/Code/PowerShell/Misc/foo/dfsdfg.ps1"},"lines":[2],"breakpoints":[{"line":2}],"sourceModified":false},"type":"request","seq":3}
194196 SetBreakpointsResponse setBreakpointsResponse = await PsesDebugAdapterClient . SetBreakpoints ( new SetBreakpointsArguments
195197 {
196198 Source = new Source { Name = Path . GetFileName ( filePath ) , Path = filePath } ,
197199 Breakpoints = new SourceBreakpoint [ ] { new SourceBreakpoint { Line = 2 } } ,
198200 SourceModified = false ,
199- } ) . ConfigureAwait ( false ) ;
201+ } ) . ConfigureAwait ( true ) ;
200202
201203 Breakpoint breakpoint = setBreakpointsResponse . Breakpoints . First ( ) ;
202204 Assert . True ( breakpoint . Verified ) ;
203205 Assert . Equal ( filePath , breakpoint . Source . Path , ignoreCase : s_isWindows ) ;
204206 Assert . Equal ( 2 , breakpoint . Line ) ;
205207
206- ConfigurationDoneResponse configDoneResponse = await PsesDebugAdapterClient . RequestConfigurationDone ( new ConfigurationDoneArguments ( ) ) . ConfigureAwait ( false ) ;
208+ ConfigurationDoneResponse configDoneResponse = await PsesDebugAdapterClient . RequestConfigurationDone ( new ConfigurationDoneArguments ( ) ) . ConfigureAwait ( true ) ;
207209 Assert . NotNull ( configDoneResponse ) ;
208- await Task . Delay ( 2000 ) . ConfigureAwait ( false ) ;
209-
210- string [ ] log = GetLog ( ) ;
211- Assert . Single ( log , ( i ) => i == "before breakpoint" ) ;
210+ Assert . Collection ( await GetLog ( ) . ConfigureAwait ( true ) ,
211+ ( i ) => Assert . Equal ( "before breakpoint" , i ) ) ;
212+ File . Delete ( s_testOutputPath ) ;
212213
213214 ContinueResponse continueResponse = await PsesDebugAdapterClient . RequestContinue (
214215 new ContinueArguments { ThreadId = 1 } ) . ConfigureAwait ( true ) ;
215216
216217 Assert . NotNull ( continueResponse ) ;
217- await Task . Delay ( 2000 ) . ConfigureAwait ( false ) ;
218-
219- log = GetLog ( ) ;
220- Assert . Collection ( log ,
221- ( i ) => Assert . Equal ( "before breakpoint" , i ) ,
218+ Assert . Collection ( await GetLog ( ) . ConfigureAwait ( true ) ,
222219 ( i ) => Assert . Equal ( "at breakpoint" , i ) ,
223220 ( i ) => Assert . Equal ( "after breakpoint" , i ) ) ;
224221 }
@@ -247,24 +244,24 @@ public async Task CanStepPastSystemWindowsForms()
247244 "Write-Host $form"
248245 } ) ) ;
249246
250- await PsesDebugAdapterClient . LaunchScript ( filePath , Started ) . ConfigureAwait ( false ) ;
247+ await PsesDebugAdapterClient . LaunchScript ( filePath , Started ) . ConfigureAwait ( true ) ;
251248
252249 SetFunctionBreakpointsResponse setBreakpointsResponse = await PsesDebugAdapterClient . SetFunctionBreakpoints (
253250 new SetFunctionBreakpointsArguments
254251 {
255252 Breakpoints = new FunctionBreakpoint [ ]
256253 { new FunctionBreakpoint { Name = "Write-Host" , } }
257- } ) . ConfigureAwait ( false ) ;
254+ } ) . ConfigureAwait ( true ) ;
258255
259256 Breakpoint breakpoint = setBreakpointsResponse . Breakpoints . First ( ) ;
260257 Assert . True ( breakpoint . Verified ) ;
261258
262- ConfigurationDoneResponse configDoneResponse = await PsesDebugAdapterClient . RequestConfigurationDone ( new ConfigurationDoneArguments ( ) ) . ConfigureAwait ( false ) ;
259+ ConfigurationDoneResponse configDoneResponse = await PsesDebugAdapterClient . RequestConfigurationDone ( new ConfigurationDoneArguments ( ) ) . ConfigureAwait ( true ) ;
263260 Assert . NotNull ( configDoneResponse ) ;
264- await Task . Delay ( 2000 ) . ConfigureAwait ( false ) ;
261+ await Task . Delay ( 5000 ) . ConfigureAwait ( true ) ;
265262
266263 VariablesResponse variablesResponse = await PsesDebugAdapterClient . RequestVariables (
267- new VariablesArguments { VariablesReference = 1 } ) . ConfigureAwait ( false ) ;
264+ new VariablesArguments { VariablesReference = 1 } ) . ConfigureAwait ( true ) ;
268265
269266 Variable form = variablesResponse . Variables . FirstOrDefault ( v => v . Name == "$form" ) ;
270267 Assert . NotNull ( form ) ;
@@ -286,13 +283,12 @@ public async Task CanLaunchScriptWithCommentedLastLineAsync()
286283 // PsesLaunchRequestArguments.Script, which is then assigned to
287284 // DebugStateService.ScriptToLaunch in that handler, and finally used by the
288285 // ConfigurationDoneHandler in LaunchScriptAsync.
289- await PsesDebugAdapterClient . LaunchScript ( script , Started ) . ConfigureAwait ( false ) ;
286+ await PsesDebugAdapterClient . LaunchScript ( script , Started ) . ConfigureAwait ( true ) ;
290287
291- ConfigurationDoneResponse configDoneResponse = await PsesDebugAdapterClient . RequestConfigurationDone ( new ConfigurationDoneArguments ( ) ) . ConfigureAwait ( false ) ;
288+ ConfigurationDoneResponse configDoneResponse = await PsesDebugAdapterClient . RequestConfigurationDone ( new ConfigurationDoneArguments ( ) ) . ConfigureAwait ( true ) ;
292289 Assert . NotNull ( configDoneResponse ) ;
293- await Task . Delay ( 2000 ) . ConfigureAwait ( false ) ;
294-
295- Assert . Collection ( GetLog ( ) , ( i ) => Assert . Equal ( "a log statement" , i ) ) ;
290+ Assert . Collection ( await GetLog ( ) . ConfigureAwait ( true ) ,
291+ ( i ) => Assert . Equal ( "a log statement" , i ) ) ;
296292 }
297293 }
298294}
0 commit comments