diff --git a/airbyte-cdk/bulk/core/base/src/main/kotlin/io/airbyte/cdk/AirbyteConnectorRunner.kt b/airbyte-cdk/bulk/core/base/src/main/kotlin/io/airbyte/cdk/AirbyteConnectorRunner.kt index bd61711e3745..41b8bf1f1bdf 100644 --- a/airbyte-cdk/bulk/core/base/src/main/kotlin/io/airbyte/cdk/AirbyteConnectorRunner.kt +++ b/airbyte-cdk/bulk/core/base/src/main/kotlin/io/airbyte/cdk/AirbyteConnectorRunner.kt @@ -58,7 +58,18 @@ sealed class AirbyteConnectorRunner( val testBeanDefinitions: Array>, val testProperties: Map = emptyMap(), ) { - val envs: Array = arrayOf(Environment.CLI, connectorType) + // Micronaut's TEST env detection relies on inspecting the stacktrace and checking for + // any junit calls. This doesn't work if we launch the connector from a different thread, e.g. + // `Dispatchers.IO`. Force the test env if needed. (Some tests launch the connector from the IO + // context to avoid blocking themselves.) + private val isTest = testBeanDefinitions.isNotEmpty() + val envs: Array = + arrayOf(Environment.CLI, connectorType) + + if (isTest) { + arrayOf(Environment.TEST) + } else { + emptyArray() + } inline fun run() { val picocliCommandLineFactory = PicocliCommandLineFactory(this) @@ -82,6 +93,10 @@ sealed class AirbyteConnectorRunner( ) .beanDefinitions(*testBeanDefinitions) .start() + // We can't rely on the isTest value from our constructor, + // because that won't autodetect junit in our stacktrace. + // So instead we ask micronaut (which will include if we explicitly added + // the TEST env). val isTest: Boolean = ctx.environment.activeNames.contains(Environment.TEST) val picocliFactory: CommandLine.IFactory = MicronautFactory(ctx) val picocliCommandLine: CommandLine = diff --git a/airbyte-cdk/bulk/core/base/src/testFixtures/kotlin/io/airbyte/cdk/command/CliRunner.kt b/airbyte-cdk/bulk/core/base/src/testFixtures/kotlin/io/airbyte/cdk/command/CliRunner.kt index 20a0b68c8c61..44950c69fe0f 100644 --- a/airbyte-cdk/bulk/core/base/src/testFixtures/kotlin/io/airbyte/cdk/command/CliRunner.kt +++ b/airbyte-cdk/bulk/core/base/src/testFixtures/kotlin/io/airbyte/cdk/command/CliRunner.kt @@ -65,7 +65,7 @@ data object CliRunner { args, testProperties, inputBeanDefinition, - out.beanDefinition + out.beanDefinition, ) } return CliRunnable(runnable, out.results)