@@ -6,9 +6,13 @@ import datadog.trace.api.config.CiVisibilityConfig
66import datadog.trace.api.config.GeneralConfig
77import datadog.trace.api.config.TraceInstrumentationConfig
88import datadog.trace.api.config.TracerConfig
9+ import java.nio.file.Paths
910import spock.lang.Specification
11+ import spock.lang.TempDir
1012import spock.util.environment.Jvm
1113
14+ import java.nio.file.Path
15+
1216import static datadog.trace.util.ConfigStrings.propertyNameToSystemPropertyName
1317
1418abstract class CiVisibilitySmokeTest extends Specification {
@@ -21,6 +25,9 @@ abstract class CiVisibilitySmokeTest extends Specification {
2125
2226 private static final Map<String ,String > DEFAULT_TRACER_CONFIG = defaultJvmArguments()
2327
28+ @TempDir
29+ protected Path prefsDir
30+
2431 protected static String buildJavaHome () {
2532 if (Jvm . current. isJava8()) {
2633 return System . getenv(" JAVA_8_HOME" )
@@ -69,6 +76,9 @@ abstract class CiVisibilitySmokeTest extends Specification {
6976
7077 protected List<String > buildJvmArguments (String mockBackendIntakeUrl , String serviceName , Map<String , String > additionalArgs ) {
7178 List<String > arguments = []
79+
80+ arguments + = preventJulPrefsFileLock()
81+
7282 Map<String , String > argMap = buildJvmArgMap(mockBackendIntakeUrl, serviceName, additionalArgs)
7383
7484 // for convenience when debugging locally
@@ -85,6 +95,28 @@ abstract class CiVisibilitySmokeTest extends Specification {
8595 return arguments
8696 }
8797
98+ /**
99+ * Trick to prevent jul Prefs file lock issue on forked processes, in particular in CI which
100+ * runs on Linux and have competing processes trying to write to it, including the Gradle daemon.
101+ *
102+ * <pre ><code >
103+ * Couldn't flush user prefs: java.util.prefs.BackingStoreException: Couldn't get file lock.
104+ * </code></pre>
105+ *
106+ * Note, some tests can setup arguments on spec level, so `prefsDir` will be `null` during
107+ * `setupSpec()`.
108+ */
109+ protected String preventJulPrefsFileLock () {
110+ String prefsPath = (prefsDir ?: tempUserPrefsPath()). toAbsolutePath()
111+ return " -Djava.util.prefs.userRoot=$prefsPath " . toString()
112+ }
113+
114+ private static Path tempUserPrefsPath () {
115+ String uniqueId = " ${ System.currentTimeMillis()} _${ System.nanoTime()} _${ Thread.currentThread().id} "
116+ Path prefsPath = Paths . get(System . getProperty(" java.io.tmpdir" ), " gradle-test-userPrefs" , uniqueId)
117+ return prefsPath
118+ }
119+
88120 protected verifyEventsAndCoverages (String projectName , String toolchain , String toolchainVersion , List<Map<String , Object > > events , List<Map<String , Object > > coverages , List<String > additionalDynamicTags = []) {
89121 def additionalReplacements = [" content.meta.['test.toolchain']" : " $toolchain :$toolchainVersion " ]
90122
0 commit comments