From 54d9fbd07f8f6b8495a3d2d8003ccb6a7d211909 Mon Sep 17 00:00:00 2001 From: "ah.jo" Date: Fri, 15 Sep 2023 14:40:59 +0900 Subject: [PATCH] Fix Kotlin PrimaryConstructorArbitraryIntrospector generates concurrently --- ...PrimaryConstructorArbitraryIntrospector.kt | 10 +++--- .../kotlin-concurrent-tests/build.gradle | 10 ++++++ .../tests/concurrent/kotlin/ConcurrentTest.kt | 36 +++++++++++++++++++ .../test/resources/junit-platform.properties | 4 +++ settings.gradle | 3 +- 5 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 fixture-monkey-tests/kotlin-concurrent-tests/build.gradle create mode 100644 fixture-monkey-tests/kotlin-concurrent-tests/src/test/kotlin/com/navercorp/fixturemonkey/tests/concurrent/kotlin/ConcurrentTest.kt create mode 100644 fixture-monkey-tests/kotlin-concurrent-tests/src/test/resources/junit-platform.properties diff --git a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/PrimaryConstructorArbitraryIntrospector.kt b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/PrimaryConstructorArbitraryIntrospector.kt index f4a7bb97a..e9b610900 100644 --- a/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/PrimaryConstructorArbitraryIntrospector.kt +++ b/fixture-monkey-kotlin/src/main/kotlin/com/navercorp/fixturemonkey/kotlin/introspector/PrimaryConstructorArbitraryIntrospector.kt @@ -46,15 +46,15 @@ class PrimaryConstructorArbitraryIntrospector : ArbitraryIntrospector { return ArbitraryIntrospectorResult.NOT_INTROSPECTED } - val kotlinClass = Reflection.createKotlinClass(type) as KClass<*> - val constructor = CONSTRUCTOR_CACHE.computeIfAbsent(type) { - requireNotNull(kotlinClass.primaryConstructor) { "No kotlin primary constructor provided for $kotlinClass" } - } - return ArbitraryIntrospectorResult( CombinableArbitrary.objectBuilder() .properties(context.combinableArbitrariesByArbitraryProperty) .build { + val kotlinClass = Reflection.createKotlinClass(type) as KClass<*> + val constructor = CONSTRUCTOR_CACHE.computeIfAbsent(type) { + requireNotNull(kotlinClass.primaryConstructor) { "No kotlin primary constructor provided for $kotlinClass" } + } + val arbitrariesByPropertyName = it.mapKeys { map -> map.key.objectProperty.property.name } val map = mutableMapOf() diff --git a/fixture-monkey-tests/kotlin-concurrent-tests/build.gradle b/fixture-monkey-tests/kotlin-concurrent-tests/build.gradle new file mode 100644 index 000000000..88f230787 --- /dev/null +++ b/fixture-monkey-tests/kotlin-concurrent-tests/build.gradle @@ -0,0 +1,10 @@ +plugins { + id "org.jetbrains.kotlin.jvm" version "${KOTLIN_VERSION}" +} + +dependencies { + implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${KOTLIN_VERSION}") + implementation("org.jetbrains.kotlin:kotlin-reflect:${KOTLIN_VERSION}") + + testImplementation(project(":fixture-monkey-kotlin")) +} diff --git a/fixture-monkey-tests/kotlin-concurrent-tests/src/test/kotlin/com/navercorp/fixturemonkey/tests/concurrent/kotlin/ConcurrentTest.kt b/fixture-monkey-tests/kotlin-concurrent-tests/src/test/kotlin/com/navercorp/fixturemonkey/tests/concurrent/kotlin/ConcurrentTest.kt new file mode 100644 index 000000000..c0e52a827 --- /dev/null +++ b/fixture-monkey-tests/kotlin-concurrent-tests/src/test/kotlin/com/navercorp/fixturemonkey/tests/concurrent/kotlin/ConcurrentTest.kt @@ -0,0 +1,36 @@ +package com.navercorp.fixturemonkey.tests.concurrent.kotlin + +import com.navercorp.fixturemonkey.FixtureMonkey +import com.navercorp.fixturemonkey.kotlin.KotlinPlugin +import com.navercorp.fixturemonkey.kotlin.giveMeOne +import com.navercorp.fixturemonkey.tests.TestEnvironment +import org.assertj.core.api.BDDAssertions.then +import org.junit.jupiter.api.RepeatedTest + +class ConcurrentTest { + @RepeatedTest(TestEnvironment.TEST_COUNT) + fun test1() { + val actual: KotlinObject = SUT.giveMeOne() + then(actual).isNotNull() + } + + @RepeatedTest(TestEnvironment.TEST_COUNT) + fun test2() { + val actual: KotlinObject = SUT.giveMeOne() + then(actual).isNotNull() + } + + @RepeatedTest(TestEnvironment.TEST_COUNT) + fun test3() { + val actual: KotlinObject = SUT.giveMeOne() + then(actual).isNotNull() + } + + data class KotlinObject(val value: String, val map: Map) + + companion object { + private val SUT = FixtureMonkey.builder() + .plugin(KotlinPlugin()) + .build() + } +} diff --git a/fixture-monkey-tests/kotlin-concurrent-tests/src/test/resources/junit-platform.properties b/fixture-monkey-tests/kotlin-concurrent-tests/src/test/resources/junit-platform.properties new file mode 100644 index 000000000..28d51a24e --- /dev/null +++ b/fixture-monkey-tests/kotlin-concurrent-tests/src/test/resources/junit-platform.properties @@ -0,0 +1,4 @@ +junit.jupiter.execution.parallel.enabled=true +junit.jupiter.execution.parallel.mode.default=CONCURRENT +junit.jupiter.execution.parallel.config.strategy=fixed +junit.jupiter.execution.parallel.config.fixed.parallelism=4 diff --git a/settings.gradle b/settings.gradle index ee7a9baa6..6f2123479 100644 --- a/settings.gradle +++ b/settings.gradle @@ -17,6 +17,7 @@ include( "fixture-monkey-tests:java-17-tests", "fixture-monkey-tests:kotlin-tests", "fixture-monkey-tests:java-tests", - "fixture-monkey-tests:java-concurrent-tests" + "fixture-monkey-tests:java-concurrent-tests", + "fixture-monkey-tests:kotlin-concurrent-tests" )