@@ -242,14 +242,42 @@ private static async Task Test_AsyncRelayCommandOfT_AllowConcurrentExecutions_Te
242
242
_ = Assert . ThrowsException < InvalidCastException > ( ( ) => command . Execute ( new object ( ) ) ) ;
243
243
}
244
244
245
+ [ TestMethod ]
246
+ public void Test_AsyncRelayCommandOfT_EnsureExceptionThrown_Synchronously ( )
247
+ {
248
+ Exception ? executeException = null ;
249
+
250
+ AsyncRelayCommand < int > command = new ( async delay =>
251
+ {
252
+ await Task . CompletedTask ;
253
+
254
+ throw new Exception ( nameof ( Test_AsyncRelayCommandOfT_EnsureExceptionThrown_Synchronously ) ) ;
255
+ } ) ;
256
+
257
+ try
258
+ {
259
+ AsyncContext . Run ( async ( ) =>
260
+ {
261
+ command . Execute ( ( object ) 42 ) ;
262
+
263
+ await Task . Delay ( 500 ) ;
264
+ } ) ;
265
+ }
266
+ catch ( Exception e )
267
+ {
268
+ executeException = e ;
269
+ }
270
+
271
+ Assert . AreEqual ( nameof ( Test_AsyncRelayCommandOfT_EnsureExceptionThrown_Synchronously ) , executeException ? . Message ) ;
272
+ }
273
+
245
274
// See https://github.com/CommunityToolkit/dotnet/pull/251
246
275
[ TestMethod ]
247
276
public async Task Test_AsyncRelayCommandOfT_EnsureExceptionThrown ( )
248
277
{
249
278
const int delay = 500 ;
250
279
251
280
Exception ? executeException = null ;
252
- Exception ? executeTException = null ;
253
281
Exception ? executeAsyncException = null ;
254
282
255
283
AsyncRelayCommand < int > command = new ( async delay =>
@@ -273,6 +301,27 @@ public async Task Test_AsyncRelayCommandOfT_EnsureExceptionThrown()
273
301
executeException = e ;
274
302
}
275
303
304
+ executeAsyncException = await Assert . ThrowsExceptionAsync < Exception > ( ( ) => command . ExecuteAsync ( ( object ) delay ) ) ;
305
+
306
+ Assert . AreEqual ( nameof ( Test_AsyncRelayCommandOfT_EnsureExceptionThrown ) , executeException ? . Message ) ;
307
+ Assert . AreEqual ( nameof ( Test_AsyncRelayCommandOfT_EnsureExceptionThrown ) , executeAsyncException ? . Message ) ;
308
+ }
309
+
310
+ [ TestMethod ]
311
+ public async Task Test_AsyncRelayCommandOfT_EnsureExceptionThrown_GenericExecute ( )
312
+ {
313
+ const int delay = 500 ;
314
+
315
+ Exception ? executeTException = null ;
316
+ Exception ? executeAsyncException = null ;
317
+
318
+ AsyncRelayCommand < int > command = new ( async delay =>
319
+ {
320
+ await Task . Delay ( delay ) ;
321
+
322
+ throw new Exception ( nameof ( Test_AsyncRelayCommandOfT_EnsureExceptionThrown_GenericExecute ) ) ;
323
+ } ) ;
324
+
276
325
try
277
326
{
278
327
AsyncContext . Run ( async ( ) =>
@@ -289,9 +338,8 @@ public async Task Test_AsyncRelayCommandOfT_EnsureExceptionThrown()
289
338
290
339
executeAsyncException = await Assert . ThrowsExceptionAsync < Exception > ( ( ) => command . ExecuteAsync ( delay ) ) ;
291
340
292
- Assert . AreEqual ( nameof ( Test_AsyncRelayCommandOfT_EnsureExceptionThrown ) , executeException ? . Message ) ;
293
- Assert . AreEqual ( nameof ( Test_AsyncRelayCommandOfT_EnsureExceptionThrown ) , executeTException ? . Message ) ;
294
- Assert . AreEqual ( nameof ( Test_AsyncRelayCommandOfT_EnsureExceptionThrown ) , executeAsyncException ? . Message ) ;
341
+ Assert . AreEqual ( nameof ( Test_AsyncRelayCommandOfT_EnsureExceptionThrown_GenericExecute ) , executeTException ? . Message ) ;
342
+ Assert . AreEqual ( nameof ( Test_AsyncRelayCommandOfT_EnsureExceptionThrown_GenericExecute ) , executeAsyncException ? . Message ) ;
295
343
}
296
344
297
345
[ TestMethod ]
@@ -320,6 +368,32 @@ async void TestCallback(Action throwAction, Action completeAction)
320
368
Assert . IsTrue ( success ) ;
321
369
}
322
370
371
+ [ TestMethod ]
372
+ public async Task Test_AsyncRelayCommandOfT_ThrowingTaskBubblesToUnobservedTaskException_Synchronously ( )
373
+ {
374
+ static async Task TestMethodAsync ( Action action )
375
+ {
376
+ await Task . CompletedTask ;
377
+
378
+ action ( ) ;
379
+ }
380
+
381
+ async void TestCallback ( Action throwAction , Action completeAction )
382
+ {
383
+ AsyncRelayCommand < string > command = new ( s => TestMethodAsync ( throwAction ) , AsyncRelayCommandOptions . FlowExceptionsToTaskScheduler ) ;
384
+
385
+ command . Execute ( null ) ;
386
+
387
+ await Task . Delay ( 200 ) ;
388
+
389
+ completeAction ( ) ;
390
+ }
391
+
392
+ bool success = await TaskSchedulerTestHelper . IsExceptionBubbledUpToUnobservedTaskExceptionAsync ( TestCallback ) ;
393
+
394
+ Assert . IsTrue ( success ) ;
395
+ }
396
+
323
397
public async Task Test_AsyncRelayCommand_ExecuteDoesNotRaiseCanExecuteChanged ( )
324
398
{
325
399
TaskCompletionSource < object ? > tcs = new ( ) ;
0 commit comments