From 4b66658d03a0333fe2bd952ebc70682494237abf Mon Sep 17 00:00:00 2001 From: "Konstantin.Nisht" Date: Fri, 3 Jan 2025 18:08:25 +0100 Subject: [PATCH] [platform] Handle exceptions in junit5 fixtures GitOrigin-RevId: 891f6f19b2563f64547e81d75f3878e4c1a0c389 --- .../src/fixture/TestFixtureExtension.kt | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/platform/testFramework/junit5/src/fixture/TestFixtureExtension.kt b/platform/testFramework/junit5/src/fixture/TestFixtureExtension.kt index 66f9aec5278a..f4461978e6b1 100644 --- a/platform/testFramework/junit5/src/fixture/TestFixtureExtension.kt +++ b/platform/testFramework/junit5/src/fixture/TestFixtureExtension.kt @@ -40,10 +40,7 @@ internal class TestFixtureExtension : BeforeAllCallback, val fixture = field.get(testInstance) as TestFixtureImpl<*> pendingFixtures.add(fixture.init(testScope, context.uniqueId)) } - @Suppress("SSBasedInspection") - runBlocking { - pendingFixtures.awaitAll() - } + awaitFixtureInitialization(testScope, pendingFixtures) context.getStore(ExtensionContext.Namespace.GLOBAL).put("TestFixtureExtension", testScope) } @@ -63,3 +60,21 @@ internal class TestFixtureExtension : BeforeAllCallback, } } } + +private fun awaitFixtureInitialization(cleanupScope: CoroutineScope, pendingFixtures: List>) { + @Suppress("SSBasedInspection") + runBlocking { + try { + pendingFixtures.awaitAll() + } + catch (e: Throwable) { + try { + cleanupScope.coroutineContext.job.cancelAndJoin() + } + catch (exceptionDuringCleanup: Throwable) { + e.addSuppressed(Throwable("Exception during cleanup of test fixture", exceptionDuringCleanup)) + } + throw e + } + } +} \ No newline at end of file