22// SPDX-License-Identifier: MIT
33
44using System . Collections . ObjectModel ;
5- using Moq . Protected ;
5+ using System . Linq . Expressions ;
66
77namespace PSql . Deploy ;
88
99[ TestFixture ]
1010public class DeploymentSessionTests : TestHarnessBase
1111{
12- private Mock < DeploymentSession > ? _session ;
12+ private Mock < TestDeploymentSession > ? _session ;
1313
1414 private readonly DeploymentSessionOptions
1515 _options = new TestDeploymentSessionOptions ( ) ;
1616
17- private Mock < DeploymentSession > SessionMock
17+ private Mock < TestDeploymentSession > SessionMock
1818 => _session ??= CreateSession ( ) ;
1919
2020 private DeploymentSession Session
@@ -81,7 +81,15 @@ public void BeginApplying_Group_NullTarget()
8181 [ Test ]
8282 public async Task Apply_Target_Ok ( )
8383 {
84- ExpectApplyCore ( TargetA , maxParallelism : 4 ) ;
84+ static void AssertParallelism ( Target t , Parallelism p )
85+ {
86+ p . MaxParallelTargets . ShouldBe ( 1 ) ;
87+ p . MaxParallelCommands . ShouldBe ( 4 ) ;
88+ p . MaxCommandsPerTarget . ShouldBe ( 4 ) ;
89+ }
90+
91+ ExpectGetMaxParallelTargets ( g => g . Targets . Single ( ) == TargetA , result : 1 ) ;
92+ ExpectApplyCore ( TargetA , AssertParallelism ) ;
8593
8694 Session . BeginApplying ( TargetA , maxParallelism : 4 ) ;
8795
@@ -94,11 +102,19 @@ public async Task Apply_Group_Ok()
94102 var group = new TargetGroup (
95103 [ TargetA , TargetB ] ,
96104 maxParallelism : 8 ,
97- maxParallelismPerTarget : 2
105+ maxParallelismPerTarget : 1
98106 ) ;
99107
100- ExpectApplyCore ( TargetA , maxParallelism : 2 ) ;
101- ExpectApplyCore ( TargetB , maxParallelism : 2 ) ;
108+ static void AssertParallelism ( Target t , Parallelism p )
109+ {
110+ p . MaxParallelTargets . ShouldBe ( 6 ) ;
111+ p . MaxParallelCommands . ShouldBe ( 8 ) ;
112+ p . MaxCommandsPerTarget . ShouldBe ( 1 ) ;
113+ }
114+
115+ ExpectGetMaxParallelTargets ( g => g == group , result : 6 ) ;
116+ ExpectApplyCore ( TargetA , AssertParallelism ) ;
117+ ExpectApplyCore ( TargetB , AssertParallelism ) ;
102118
103119 Session . BeginApplying ( group ) ;
104120
@@ -108,8 +124,8 @@ public async Task Apply_Group_Ok()
108124 [ Test ]
109125 public async Task Apply_Any_Cancellation ( )
110126 {
111- ExpectApplyCore ( TargetA , maxParallelism : 1 , cancel : true ) ; // cancels session
112- //pectApplyCore(TargetB, maxParallelism: 1); // never happens
127+ ExpectApplyCore ( TargetA , ( t , p ) => Session . Cancel ( ) ) ; // cancels session
128+ //pectApplyCore(TargetB); // never happens
113129
114130 Session . BeginApplying ( TargetA , maxParallelism : 1 ) ;
115131 await WaitForSessionCancellationAsync ( ) ; // because count of errors exceeded max (default 0)
@@ -129,9 +145,9 @@ public async Task Apply_Any_Exception_UpToMax()
129145 _options . MaxErrorCount = 1 ;
130146 var exception = new Exception ( "Oops!" ) ;
131147
132- ExpectApplyCore ( TargetA , maxParallelism : 1 , exception ) ; // error tolerated
133- ExpectApplyCore ( TargetB , maxParallelism : 1 ) ; // succeeds
134- ExpectApplyCore ( TargetC , maxParallelism : 1 ) ; // succeeds
148+ ExpectApplyCore ( TargetA , ( t , p ) => throw exception ) ; // error tolerated
149+ ExpectApplyCore ( TargetB ) ; // succeeds
150+ ExpectApplyCore ( TargetC ) ; // succeeds
135151
136152 Session . BeginApplying ( TargetA , maxParallelism : 1 ) ;
137153 await WaitForSessionToHaveErrorsAsync ( ) ;
@@ -154,9 +170,9 @@ public async Task Apply_Any_Exception_OverMax()
154170 var exceptionA = new Exception ( "Bam!" ) ;
155171 var exceptionB = new Exception ( "Pow!" ) ;
156172
157- ExpectApplyCore ( TargetA , maxParallelism : 1 , exceptionA ) ; // error tolerated
158- ExpectApplyCore ( TargetB , maxParallelism : 1 , exceptionB ) ; // error cancels session
159- //pectApplyCore(TargetC, maxParallelism: 1); // never happens
173+ ExpectApplyCore ( TargetA , ( t , p ) => throw exceptionA ) ; // error tolerated
174+ ExpectApplyCore ( TargetB , ( t , p ) => throw exceptionB ) ; // error cancels session
175+ //pectApplyCore(TargetC); // never happens
160176
161177 Session . BeginApplying ( TargetA , maxParallelism : 1 ) ;
162178 await WaitForSessionToHaveErrorsAsync ( ) ;
@@ -187,7 +203,7 @@ public async Task Apply_Any_Exception_ReadOnlyData()
187203
188204 var exception = new ExceptionWithData ( data ) ;
189205
190- ExpectApplyCore ( TargetA , maxParallelism : 1 , exception ) ;
206+ ExpectApplyCore ( TargetA , ( t , p ) => throw exception ) ;
191207
192208 Session . BeginApplying ( TargetA , maxParallelism : 1 ) ;
193209
@@ -204,7 +220,7 @@ public async Task Apply_Any_Exception_NullData()
204220 {
205221 var exception = new ExceptionWithData ( data : null ) ;
206222
207- ExpectApplyCore ( TargetA , maxParallelism : 1 , exception ) ;
223+ ExpectApplyCore ( TargetA , ( t , p ) => throw exception ) ;
208224
209225 Session . BeginApplying ( TargetA , maxParallelism : 1 ) ;
210226
@@ -216,37 +232,29 @@ public async Task Apply_Any_Exception_NullData()
216232 thrown . ShouldBeSameAs ( exception ) ;
217233 }
218234
219- private void ExpectApplyCore ( Target target , int maxParallelism )
235+ private void ExpectGetMaxParallelTargets (
236+ Expression < Func < TargetGroup , bool > > predicate , int result )
220237 {
221238 SessionMock
222- . Protected ( )
223- . Setup < Task > ( "ApplyCoreAsync" , target , maxParallelism )
224- . Returns ( Task . CompletedTask )
239+ . Setup ( s => s . PublicGetMaxParallelTargets_Public ( It . Is ( predicate ) ) )
240+ . Returns ( result )
225241 . Verifiable ( ) ;
226242 }
227243
228- private void ExpectApplyCore ( Target target , int maxParallelism , bool cancel )
244+ private void ExpectApplyCore ( Target target , Action < Target , Parallelism > ? callback = null )
229245 {
230- SessionMock
231- . Protected ( )
232- . Setup < Task > ( "ApplyCoreAsync" , target , maxParallelism )
233- . Callback ( Session . Cancel )
234- . ThrowsAsync ( new OperationCanceledException ( ) )
235- . Verifiable ( ) ;
236- }
246+ static void Nop ( Target t , Parallelism p ) { }
237247
238- private void ExpectApplyCore ( Target target , int maxParallelism , Exception exception )
239- {
240248 SessionMock
241- . Protected ( )
242- . Setup < Task > ( "ApplyCoreAsync" , target , maxParallelism )
243- . ThrowsAsync ( exception )
249+ . Setup ( s => s . ApplyCoreAsync_Public ( target , It . IsNotNull < Parallelism > ( ) ) )
250+ . Callback ( callback ?? Nop )
251+ . Returns ( Task . CompletedTask )
244252 . Verifiable ( ) ;
245253 }
246254
247- private Mock < DeploymentSession > CreateSession ( )
255+ private Mock < TestDeploymentSession > CreateSession ( )
248256 {
249- var session = Mocks . Create < DeploymentSession > ( MockBehavior . Loose , _options ) ;
257+ var session = Mocks . Create < TestDeploymentSession > ( MockBehavior . Loose , _options ) ;
250258
251259 session . CallBase = true ;
252260
@@ -280,14 +288,23 @@ protected override void CleanUp(bool managed)
280288 base . CleanUp ( managed ) ;
281289 }
282290
283- private class TestDeploymentSessionOptions : DeploymentSessionOptions { }
291+ internal class TestDeploymentSessionOptions : DeploymentSessionOptions { }
284292
285- private class TestDeploymentSession : DeploymentSession
293+ internal class TestDeploymentSession : DeploymentSession
286294 {
287295 public TestDeploymentSession ( TestDeploymentSessionOptions options )
288296 : base ( options ) { }
289297
290- protected override Task ApplyCoreAsync ( Target target , int maxParallelism )
298+ protected sealed override int GetMaxParallelTargets ( TargetGroup group )
299+ => PublicGetMaxParallelTargets_Public ( group ) ;
300+
301+ protected sealed override Task ApplyCoreAsync ( Target target , Parallelism parallelism )
302+ => ApplyCoreAsync_Public ( target , parallelism ) ;
303+
304+ public virtual int PublicGetMaxParallelTargets_Public ( TargetGroup group )
305+ => 4 ;
306+
307+ public virtual Task ApplyCoreAsync_Public ( Target target , Parallelism parallelism )
291308 => Task . CompletedTask ;
292309 }
293310
0 commit comments