@@ -39,7 +39,7 @@ public async Task AfterInitialization_Is_Invoked_For_Setting_Default_Provider()
3939 providerMock . Status . Returns ( ProviderStatus . NotReady ) ;
4040 var context = new EvaluationContextBuilder ( ) . Build ( ) ;
4141 var callCount = 0 ;
42- await repository . SetProviderAsync ( providerMock , context , afterInitSuccess : ( theProvider ) =>
42+ await repository . SetProviderAsync ( providerMock , context , afterInitSuccess : ( theProvider , ct ) =>
4343 {
4444 Assert . Equal ( providerMock , theProvider ) ;
4545 callCount ++ ;
@@ -48,17 +48,42 @@ await repository.SetProviderAsync(providerMock, context, afterInitSuccess: (theP
4848 Assert . Equal ( 1 , callCount ) ;
4949 }
5050
51+ [ Fact ]
52+ public async Task AfterInitialization_Is_Invoked_With_CancellationToken ( )
53+ {
54+ var repository = new ProviderRepository ( ) ;
55+ var providerMock = Substitute . For < FeatureProvider > ( ) ;
56+ providerMock . Status . Returns ( ProviderStatus . NotReady ) ;
57+
58+ using var cancellationTokenSource = new CancellationTokenSource ( ) ;
59+ var cancellationToken = cancellationTokenSource . Token ;
60+
61+ var context = new EvaluationContextBuilder ( ) . Build ( ) ;
62+
63+ var initCancellationToken = CancellationToken . None ;
64+ await repository . SetProviderAsync ( providerMock , context , afterInitSuccess : ( theProvider , ct ) =>
65+ {
66+ Assert . Equal ( providerMock , theProvider ) ;
67+
68+ initCancellationToken = ct ;
69+
70+ return Task . CompletedTask ;
71+ } , cancellationToken : cancellationToken ) ;
72+
73+ Assert . Equal ( cancellationToken , initCancellationToken ) ;
74+ }
75+
5176 [ Fact ]
5277 public async Task AfterError_Is_Invoked_If_Initialization_Errors_Default_Provider ( )
5378 {
5479 var repository = new ProviderRepository ( ) ;
5580 var providerMock = Substitute . For < FeatureProvider > ( ) ;
5681 providerMock . Status . Returns ( ProviderStatus . NotReady ) ;
5782 var context = new EvaluationContextBuilder ( ) . Build ( ) ;
58- providerMock . When ( x => x . InitializeAsync ( context ) ) . Throw ( new Exception ( "BAD THINGS" ) ) ;
83+ providerMock . When ( x => x . InitializeAsync ( context , Arg . Any < CancellationToken > ( ) ) ) . Throw ( new Exception ( "BAD THINGS" ) ) ;
5984 var callCount = 0 ;
6085 Exception ? receivedError = null ;
61- await repository . SetProviderAsync ( providerMock , context , afterInitError : ( theProvider , error ) =>
86+ await repository . SetProviderAsync ( providerMock , context , afterInitError : ( theProvider , error , ct ) =>
6287 {
6388 Assert . Equal ( providerMock , theProvider ) ;
6489 callCount ++ ;
@@ -69,6 +94,32 @@ await repository.SetProviderAsync(providerMock, context, afterInitError: (thePro
6994 Assert . Equal ( 1 , callCount ) ;
7095 }
7196
97+ [ Fact ]
98+ public async Task AfterError_Is_Invoked_With_CancellationToken ( )
99+ {
100+ var repository = new ProviderRepository ( ) ;
101+ var providerMock = Substitute . For < FeatureProvider > ( ) ;
102+ providerMock . Status . Returns ( ProviderStatus . NotReady ) ;
103+
104+ using var cancellationTokenSource = new CancellationTokenSource ( ) ;
105+ var cancellationToken = cancellationTokenSource . Token ;
106+
107+ var context = new EvaluationContextBuilder ( ) . Build ( ) ;
108+ providerMock . When ( x => x . InitializeAsync ( context , cancellationToken ) ) . Throw ( new Exception ( "BAD THINGS" ) ) ;
109+
110+ var errorCancellationToken = CancellationToken . None ;
111+ await repository . SetProviderAsync ( providerMock , context , afterInitError : ( theProvider , error , ct ) =>
112+ {
113+ Assert . Equal ( providerMock , theProvider ) ;
114+
115+ errorCancellationToken = ct ;
116+
117+ return Task . CompletedTask ;
118+ } , cancellationToken : cancellationToken ) ;
119+
120+ Assert . Equal ( cancellationToken , errorCancellationToken ) ;
121+ }
122+
72123 [ Theory ]
73124 [ InlineData ( ProviderStatus . Ready ) ]
74125 [ InlineData ( ProviderStatus . Stale ) ]
@@ -94,7 +145,7 @@ internal async Task AfterInitialize_Is_Not_Called_For_Ready_Provider(ProviderSta
94145 providerMock . Status . Returns ( status ) ;
95146 var context = new EvaluationContextBuilder ( ) . Build ( ) ;
96147 var callCount = 0 ;
97- await repository . SetProviderAsync ( providerMock , context , afterInitSuccess : provider =>
148+ await repository . SetProviderAsync ( providerMock , context , afterInitSuccess : ( provider , ct ) =>
98149 {
99150 callCount ++ ;
100151 return Task . CompletedTask ;
@@ -150,7 +201,7 @@ public async Task AfterInitialization_Is_Invoked_For_Setting_Named_Provider()
150201 providerMock . Status . Returns ( ProviderStatus . NotReady ) ;
151202 var context = new EvaluationContextBuilder ( ) . Build ( ) ;
152203 var callCount = 0 ;
153- await repository . SetProviderAsync ( "the-name" , providerMock , context , afterInitSuccess : ( theProvider ) =>
204+ await repository . SetProviderAsync ( "the-name" , providerMock , context , afterInitSuccess : ( theProvider , ct ) =>
154205 {
155206 Assert . Equal ( providerMock , theProvider ) ;
156207 callCount ++ ;
@@ -159,17 +210,41 @@ await repository.SetProviderAsync("the-name", providerMock, context, afterInitSu
159210 Assert . Equal ( 1 , callCount ) ;
160211 }
161212
213+ [ Fact ]
214+ public async Task AfterInitialization_WithNamedProvider_Is_Invoked_With_CancellationToken ( )
215+ {
216+ var repository = new ProviderRepository ( ) ;
217+ var providerMock = Substitute . For < FeatureProvider > ( ) ;
218+ providerMock . Status . Returns ( ProviderStatus . NotReady ) ;
219+
220+ var context = new EvaluationContextBuilder ( ) . Build ( ) ;
221+ using var cancellationTokenSource = new CancellationTokenSource ( ) ;
222+ var cancellationToken = cancellationTokenSource . Token ;
223+
224+ var initCancellationToken = CancellationToken . None ;
225+ await repository . SetProviderAsync ( "the-name" , providerMock , context , afterInitSuccess : ( theProvider , ct ) =>
226+ {
227+ Assert . Equal ( providerMock , theProvider ) ;
228+
229+ initCancellationToken = ct ;
230+
231+ return Task . CompletedTask ;
232+ } , cancellationToken : cancellationToken ) ;
233+
234+ Assert . Equal ( cancellationToken , initCancellationToken ) ;
235+ }
236+
162237 [ Fact ]
163238 public async Task AfterError_Is_Invoked_If_Initialization_Errors_Named_Provider ( )
164239 {
165240 var repository = new ProviderRepository ( ) ;
166241 var providerMock = Substitute . For < FeatureProvider > ( ) ;
167242 providerMock . Status . Returns ( ProviderStatus . NotReady ) ;
168243 var context = new EvaluationContextBuilder ( ) . Build ( ) ;
169- providerMock . When ( x => x . InitializeAsync ( context ) ) . Throw ( new Exception ( "BAD THINGS" ) ) ;
244+ providerMock . When ( x => x . InitializeAsync ( context , Arg . Any < CancellationToken > ( ) ) ) . Throw ( new Exception ( "BAD THINGS" ) ) ;
170245 var callCount = 0 ;
171246 Exception ? receivedError = null ;
172- await repository . SetProviderAsync ( "the-provider" , providerMock , context , afterInitError : ( theProvider , error ) =>
247+ await repository . SetProviderAsync ( "the-provider" , providerMock , context , afterInitError : ( theProvider , error , ct ) =>
173248 {
174249 Assert . Equal ( providerMock , theProvider ) ;
175250 callCount ++ ;
@@ -180,6 +255,32 @@ await repository.SetProviderAsync("the-provider", providerMock, context, afterIn
180255 Assert . Equal ( 1 , callCount ) ;
181256 }
182257
258+ [ Fact ]
259+ public async Task AfterError_WithNamedProvider_Is_Invoked_With_CancellationToken ( )
260+ {
261+ var repository = new ProviderRepository ( ) ;
262+ var providerMock = Substitute . For < FeatureProvider > ( ) ;
263+ providerMock . Status . Returns ( ProviderStatus . NotReady ) ;
264+
265+ using var cancellationTokenSource = new CancellationTokenSource ( ) ;
266+ var cancellationToken = cancellationTokenSource . Token ;
267+
268+ var context = new EvaluationContextBuilder ( ) . Build ( ) ;
269+ providerMock . When ( x => x . InitializeAsync ( context , cancellationToken ) ) . Throw ( new Exception ( "BAD THINGS" ) ) ;
270+
271+ var errorCancellationToken = CancellationToken . None ;
272+ await repository . SetProviderAsync ( "the-provider" , providerMock , context , afterInitError : ( theProvider , error , ct ) =>
273+ {
274+ Assert . Equal ( providerMock , theProvider ) ;
275+
276+ errorCancellationToken = ct ;
277+
278+ return Task . CompletedTask ;
279+ } , cancellationToken : cancellationToken ) ;
280+
281+ Assert . Equal ( cancellationToken , errorCancellationToken ) ;
282+ }
283+
183284 [ Theory ]
184285 [ InlineData ( ProviderStatus . Ready ) ]
185286 [ InlineData ( ProviderStatus . Stale ) ]
@@ -206,7 +307,7 @@ internal async Task AfterInitialize_Is_Not_Called_For_Ready_Named_Provider(Provi
206307 var context = new EvaluationContextBuilder ( ) . Build ( ) ;
207308 var callCount = 0 ;
208309 await repository . SetProviderAsync ( "the-name" , providerMock , context ,
209- afterInitSuccess : provider =>
310+ afterInitSuccess : ( provider , ct ) =>
210311 {
211312 callCount ++ ;
212313 return Task . CompletedTask ;
0 commit comments