@@ -8,6 +8,8 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter10.Listing10_17.Tests;
88[ TestClass ]
99public partial class DisposeTests
1010{
11+ private static Mutex Mutext { get ; } = new ( false , typeof ( DisposeTests ) . FullName ) ;
12+
1113 public TestContext TestContext { get ; set ; } = null ! ; // Auto-initialized by MSTest.
1214
1315 static string Ps1DirectoryPath { get ; } =
@@ -24,8 +26,6 @@ public partial class DisposeTests
2426 public static void ClassInitialize ( TestContext testContext )
2527 {
2628 string psOutput ;
27- using Mutex mutext = new ( true , nameof ( Listing10_17 ) ) ;
28-
2929 // Clean up at the start in case the class cleanup doesn't run (due to debug for example)
3030 string testStage = "cleanup" ;
3131 Assert . AreEqual < int > ( 0 , RunPowerShellScript ( testStage , out psOutput ) ,
@@ -38,13 +38,15 @@ public static void ClassInitialize(TestContext testContext)
3838 Path . Join ( Ps1DirectoryPath , ProjectName , $ "{ ProjectName } .csproj") ;
3939 Assert . IsTrue ( File . Exists ( projectFilePath ) ,
4040 $ "The expected project file, '{ projectFilePath } ', was not created.") ;
41+
4142 }
4243
4344 [ ClassCleanup ]
4445 public static void ClassCleanup ( )
4546 {
4647 // No return check since an exception here will be ignored.
4748 RunPowerShellScript ( "cleanup" , out string _ ) ;
49+ Mutext . Dispose ( ) ;
4850 }
4951
5052 [ DataTestMethod ]
@@ -53,18 +55,25 @@ public static void ClassCleanup()
5355 [ DataRow ( "gc" , GCCalled , DisplayName = "Garbage Collected called" ) ]
5456 public void FinalizerRunsAsPredicted_ConsoleOutputIsInOrder ( string finalizerOrderOption , string expectedOutput )
5557 {
56- using Mutex mutext = new ( true , nameof ( Listing10_17 ) ) ;
57- int traceValue = 0 ;
58- string testStatus = "run" ;
59-
60- TestContext . WriteLine ( $ "Ps1Path = '{ Path . GetFullPath ( Ps1Path ) } '") ;
61- int exitCode = RunPowerShellScript (
62- testStatus , finalizerOrderOption , traceValue , out string psOutput ) ;
63-
64- Assert . AreEqual ( 0 , exitCode , $ "PowerShell Output: { psOutput } ") ;
65-
66- Assert . AreEqual < string > ( RemoveWhiteSpace ( expectedOutput ) , RemoveWhiteSpace ( psOutput ) ,
67- $ "Unexpected output from '{ Ps1Path } { traceValue } { finalizerOrderOption } { testStatus } :{ Environment . NewLine } { psOutput } ") ;
58+ try
59+ {
60+ Mutext . WaitOne ( ) ;
61+ int traceValue = 0 ;
62+ string testStatus = "run" ;
63+
64+ TestContext . WriteLine ( $ "Ps1Path = '{ Path . GetFullPath ( Ps1Path ) } '") ;
65+ int exitCode = RunPowerShellScript (
66+ testStatus , finalizerOrderOption , traceValue , out string psOutput ) ;
67+
68+ Assert . AreEqual ( 0 , exitCode , $ "PowerShell Output: { psOutput } ") ;
69+
70+ Assert . AreEqual < string > ( RemoveWhiteSpace ( expectedOutput ) , RemoveWhiteSpace ( psOutput ) ,
71+ $ "Unexpected output from '{ Ps1Path } { traceValue } { finalizerOrderOption } { testStatus } :{ Environment . NewLine } { psOutput } ") ;
72+ }
73+ finally
74+ {
75+ Mutext . ReleaseMutex ( ) ;
76+ }
6877 }
6978
7079 private static int RunPowerShellScript ( string testStage , out string psOutput ) =>
0 commit comments