77using System . Diagnostics ;
88using System . Security . Principal ;
99using Xunit ;
10+ using System . Threading . Tasks ;
1011
1112/// <summary>
1213/// NOTE: All tests checking the output file should always call Stop before checking because Stop will flush the file to disk.
@@ -16,6 +17,7 @@ namespace System.ServiceProcess.Tests
1617 [ OuterLoop ( /* Modifies machine state */ ) ]
1718 public class ServiceBaseTests : IDisposable
1819 {
20+ private const int connectionTimeout = 30000 ;
1921 private readonly TestServiceProvider _testService ;
2022
2123 private static readonly Lazy < bool > s_isElevated = new Lazy < bool > ( ( ) => AdminHelpers . IsProcessElevated ( ) ) ;
@@ -75,104 +77,102 @@ public void Cleanup()
7577 [ ConditionalFact ( nameof ( IsProcessElevated ) ) ]
7678 public void TestOnStartThenStop ( )
7779 {
80+ _testService . Client . Connect ( connectionTimeout ) ;
7881 var controller = new ServiceController ( _testService . TestServiceName ) ;
7982 AssertExpectedProperties ( controller ) ;
80- string expected =
81- @"OnStart args=
82- OnStop
83- " ;
83+
8484 controller . Stop ( ) ;
85+ Assert . Equal ( ( int ) PipeMessageByteCode . Stop , _testService . GetByte ( ) ) ;
8586 controller . WaitForStatus ( ServiceControllerStatus . Stopped ) ;
86- Assert . Equal ( expected , _testService . GetServiceOutput ( ) ) ;
8787 }
8888
8989 [ ConditionalFact ( nameof ( IsProcessElevated ) ) ]
9090 public void TestOnStartWithArgsThenStop ( )
9191 {
9292 var controller = new ServiceController ( _testService . TestServiceName ) ;
93+ _testService . Client . Connect ( connectionTimeout ) ;
9394 AssertExpectedProperties ( controller ) ;
95+
9496 controller . Stop ( ) ;
97+ Assert . Equal ( ( int ) PipeMessageByteCode . Stop , _testService . GetByte ( ) ) ;
9598 controller . WaitForStatus ( ServiceControllerStatus . Stopped ) ;
9699
97- string expected =
98- @"OnStart args=a,b,c
99- OnStop
100- " ;
101- controller . Start ( new string [ ] { "a" , "b" , "c" } ) ;
100+ controller . Start ( new string [ ] { "StartWithArguments" , "a" , "b" , "c" } ) ;
101+ _testService . Client = null ;
102+ _testService . Client . Connect ( ) ;
103+
104+ Assert . Equal ( ( int ) PipeMessageByteCode . Start , _testService . GetByte ( ) ) ;
102105 controller . WaitForStatus ( ServiceControllerStatus . Running ) ;
106+
103107 controller . Stop ( ) ;
108+ Assert . Equal ( ( int ) PipeMessageByteCode . Stop , _testService . GetByte ( ) ) ;
104109 controller . WaitForStatus ( ServiceControllerStatus . Stopped ) ;
105- Assert . Equal ( expected , _testService . GetServiceOutput ( ) ) ;
106110 }
107111
108112 [ ConditionalFact ( nameof ( IsProcessElevated ) ) ]
109113 public void TestOnPauseThenStop ( )
110114 {
115+ _testService . Client . Connect ( connectionTimeout ) ;
111116 var controller = new ServiceController ( _testService . TestServiceName ) ;
112117 AssertExpectedProperties ( controller ) ;
113- string expected =
114- @"OnStart args=
115- OnPause
116- OnStop
117- " ;
118+
118119 controller . Pause ( ) ;
120+ Assert . Equal ( ( int ) PipeMessageByteCode . Pause , _testService . GetByte ( ) ) ;
119121 controller . WaitForStatus ( ServiceControllerStatus . Paused ) ;
122+
120123 controller . Stop ( ) ;
124+ Assert . Equal ( ( int ) PipeMessageByteCode . Stop , _testService . GetByte ( ) ) ;
121125 controller . WaitForStatus ( ServiceControllerStatus . Stopped ) ;
122- Assert . Equal ( expected , _testService . GetServiceOutput ( ) ) ;
123126 }
124127
125128 [ ConditionalFact ( nameof ( IsProcessElevated ) ) ]
126129 public void TestOnPauseAndContinueThenStop ( )
127130 {
131+ _testService . Client . Connect ( connectionTimeout ) ;
128132 var controller = new ServiceController ( _testService . TestServiceName ) ;
129133 AssertExpectedProperties ( controller ) ;
130- string expected =
131- @"OnStart args=
132- OnPause
133- OnContinue
134- OnStop
135- " ;
134+
136135 controller . Pause ( ) ;
136+ Assert . Equal ( ( int ) PipeMessageByteCode . Pause , _testService . GetByte ( ) ) ;
137137 controller . WaitForStatus ( ServiceControllerStatus . Paused ) ;
138+
138139 controller . Continue ( ) ;
140+ Assert . Equal ( ( int ) PipeMessageByteCode . Continue , _testService . GetByte ( ) ) ;
139141 controller . WaitForStatus ( ServiceControllerStatus . Running ) ;
142+
140143 controller . Stop ( ) ;
144+ Assert . Equal ( ( int ) PipeMessageByteCode . Stop , _testService . GetByte ( ) ) ;
141145 controller . WaitForStatus ( ServiceControllerStatus . Stopped ) ;
142- Assert . Equal ( expected , _testService . GetServiceOutput ( ) ) ;
143146 }
144147
145148 [ ConditionalFact ( nameof ( IsProcessElevated ) ) ]
146149 public void TestOnExecuteCustomCommand ( )
147150 {
151+ _testService . Client . Connect ( connectionTimeout ) ;
148152 var controller = new ServiceController ( _testService . TestServiceName ) ;
149153 AssertExpectedProperties ( controller ) ;
150- string expected =
151- @"OnStart args=
152- OnCustomCommand command=128
153- OnStop
154- " ;
154+
155155 controller . ExecuteCommand ( 128 ) ;
156- controller . WaitForStatus ( ServiceControllerStatus . Running ) ;
156+ Assert . Equal ( 128 , _testService . GetByte ( ) ) ;
157+
157158 controller . Stop ( ) ;
159+ Assert . Equal ( ( int ) PipeMessageByteCode . Stop , _testService . GetByte ( ) ) ;
158160 controller . WaitForStatus ( ServiceControllerStatus . Stopped ) ;
159- Assert . Equal ( expected , _testService . GetServiceOutput ( ) ) ;
160161 }
161162
162163 [ ConditionalFact ( nameof ( IsProcessElevated ) ) ]
163164 public void TestOnContinueBeforePause ( )
164165 {
166+ _testService . Client . Connect ( connectionTimeout ) ;
165167 var controller = new ServiceController ( _testService . TestServiceName ) ;
166168 AssertExpectedProperties ( controller ) ;
167- string expected =
168- @"OnStart args=
169- OnStop
170- " ;
169+
171170 controller . Continue ( ) ;
172171 controller . WaitForStatus ( ServiceControllerStatus . Running ) ;
172+
173173 controller . Stop ( ) ;
174+ Assert . Equal ( ( int ) PipeMessageByteCode . Stop , _testService . GetByte ( ) ) ;
174175 controller . WaitForStatus ( ServiceControllerStatus . Stopped ) ;
175- Assert . Equal ( expected , _testService . GetServiceOutput ( ) ) ;
176176 }
177177
178178 [ ConditionalFact ( nameof ( IsElevatedAndSupportsEventLogs ) ) ]
@@ -193,7 +193,7 @@ public void LogWritten()
193193 sb . Stop ( ) ;
194194 EventLog . DeleteEventSource ( sb . ServiceName ) ;
195195 }
196- }
196+ }
197197 }
198198
199199 [ ConditionalFact ( nameof ( IsElevatedAndSupportsEventLogs ) ) ]
0 commit comments