From 92b5b7595210be7e6efe014e4339a1b169577641 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Tue, 24 Dec 2024 17:23:58 +0100 Subject: [PATCH] Standardize OCE and AggregateException handling --- .../MSTest.TestAdapter/Helpers/FixtureMethodRunner.cs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Adapter/MSTest.TestAdapter/Helpers/FixtureMethodRunner.cs b/src/Adapter/MSTest.TestAdapter/Helpers/FixtureMethodRunner.cs index 0aea633c46..a7b74c95d3 100644 --- a/src/Adapter/MSTest.TestAdapter/Helpers/FixtureMethodRunner.cs +++ b/src/Adapter/MSTest.TestAdapter/Helpers/FixtureMethodRunner.cs @@ -82,7 +82,9 @@ internal static class FixtureMethodRunner action(); return null; } - catch (OperationCanceledException) + catch (Exception ex) when + ((ex is OperationCanceledException oce && oce.CancellationToken == cancellationTokenSource.Token) + || (ex is AggregateException aggregateEx && aggregateEx.InnerExceptions.OfType().Any())) { // Ideally we would like to check that the token of the exception matches cancellationTokenSource but TestContext // instances are not well defined so we have to handle the exception entirely. @@ -156,7 +158,7 @@ internal static class FixtureMethodRunner timeout)); } catch (Exception ex) when - (ex is OperationCanceledException + ((ex is OperationCanceledException oce && oce.CancellationToken == cancellationTokenSource.Token) || (ex is AggregateException aggregateEx && aggregateEx.InnerExceptions.OfType().Any())) { return new( @@ -220,10 +222,7 @@ internal static class FixtureMethodRunner timeout)); } catch (Exception ex) when - - // This exception occurs when the cancellation happens before the task is actually started. - ((ex is TaskCanceledException tce && tce.CancellationToken == cancellationTokenSource.Token) - || (ex is OperationCanceledException oce && oce.CancellationToken == cancellationTokenSource.Token) + ((ex is OperationCanceledException oce && oce.CancellationToken == cancellationTokenSource.Token) || (ex is AggregateException aggregateEx && aggregateEx.InnerExceptions.OfType().Any())) { return new(