Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions robolectric-extension/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,27 @@ test {
systemProperty 'junit.jupiter.execution.parallel.enabled', 'true'
systemProperty 'junit.jupiter.execution.parallel.mode.default', 'same_thread'
systemProperty 'junit.jupiter.execution.parallel.mode.classes.default', 'same_thread'
systemProperty 'java.util.logging.config.file',
"${projectDir}/src/test/resources/logging.properties"
systemProperty 'java.util.logging.config.file', "${projectDir}/src/test/resources/logging.properties"
systemProperty 'robolectric.usePreinstrumentedJars', 'true'
}

tasks.test {
task concurrentTest(type: Test) {
testLogging {
showStandardStreams = true
showExceptions = true
showCauses = true
showStackTraces = true
}
systemProperty 'junit.jupiter.execution.parallel.enabled', 'true'
systemProperty 'junit.jupiter.execution.parallel.mode.default', 'same_thread'
systemProperty 'junit.jupiter.execution.parallel.mode.classes.default', 'concurrent'
systemProperty 'java.util.logging.config.file', "${projectDir}/src/test/resources/logging.properties"
systemProperty 'robolectric.usePreinstrumentedJars', 'true'
}

check.dependsOn(concurrentTest)

tasks.withType(Test.class).configureEach {
javaLauncher.set(
javaToolchains.launcherFor {
languageVersion.set(JavaLanguageVersion.of(libs.versions.jvmToolchain.get().toInteger()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import org.robolectric.internal.dependency.DependencyJar
import org.robolectric.internal.dependency.MavenArtifactFetcher
import org.robolectric.internal.dependency.MavenDependencyResolver
import org.robolectric.internal.dependency.MavenJarArtifact
import tech.apter.junit.jupiter.robolectric.internal.extensions.createLogger
import java.io.File
import java.io.IOException
import java.io.RandomAccessFile
import java.net.MalformedURLException
import java.net.URL
import java.nio.channels.FileLock
import java.nio.channels.OverlappingFileLockException
import java.util.concurrent.ExecutorService


internal class JUnit5MavenDependencyResolver private constructor(
repositoryUrl: String,
repositoryId: String,
Expand Down Expand Up @@ -44,9 +48,12 @@ internal class JUnit5MavenDependencyResolver private constructor(
val artifacts: List<Pair<DependencyJar, MavenJarArtifact>> = dependencies.map { it to MavenJarArtifact(it) }

for ((dependencyJar, artifact) in artifacts) {
if (!File(localRepositoryDir, artifact.jarPath()).exists()) {
val artifactJarFile = File(localRepositoryDir, artifact.jarPath())
if (!artifactJarFile.exists()) {
whileLocked(dependencyJar) {
mavenArtifactFetcher.fetchArtifact(artifact)
if (!artifactJarFile.exists()) {
mavenArtifactFetcher.fetchArtifact(artifact)
}
}
}
}
Expand All @@ -73,9 +80,17 @@ internal class JUnit5MavenDependencyResolver private constructor(
try {
RandomAccessFile(lockFile, "rw").use { raf ->
raf.channel.use { channel ->
channel.lock().use {
runnable.run()
var lock: FileLock? = null
while (lock == null) {
try {
lock = channel.tryLock()
} catch (e: OverlappingFileLockException) {
// Sleep for a while before retrying
Thread.sleep(100)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log the swallowed exception.

The caught OverlappingFileLockException is swallowed without logging. Consider logging this exception to aid in diagnosing potential issues.

catch (e: OverlappingFileLockException) {
    // Log the exception for diagnostic purposes
    logger.warn("OverlappingFileLockException encountered: ${e.message}")
    Thread.sleep(100)
}
Tools
detekt

[warning] 87-87: The caught exception is swallowed. The original exception could be lost.

(detekt.exceptions.SwallowedException)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log the swallowed exception.

The caught OverlappingFileLockException is swallowed without logging. Consider logging this exception to aid in diagnosing potential issues.

catch (e: OverlappingFileLockException) {
    // Log the exception for diagnostic purposes
    logger.warn("OverlappingFileLockException encountered: ${e.message}")
    Thread.sleep(100)
}
Tools
detekt

[warning] 87-87: The caught exception is swallowed. The original exception could be lost.

(detekt.exceptions.SwallowedException)

}
runnable.run()
lock.release()
}
}
} catch (e: IOException) {
Expand All @@ -85,6 +100,30 @@ internal class JUnit5MavenDependencyResolver private constructor(
}
}

private fun whileLocked(runnable: Runnable) {
val lockFile = createLockFile()
try {
RandomAccessFile(lockFile, "rw").use { raf ->
raf.channel.use { channel ->
var lock: FileLock? = null
while (lock == null) {
try {
lock = channel.tryLock()
} catch (e: OverlappingFileLockException) {
// Sleep for a while before retrying
Thread.sleep(100)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whileLocked(runnable) method is unused and should log exceptions.

The whileLocked(runnable) method is currently not used anywhere in the codebase. Consider removing it if it is not intended for future use. Additionally, the OverlappingFileLockException should be logged to aid in diagnosing potential issues.

  • Location: robolectric-extension/src/main/kotlin/tech/apter/junit/jupiter/robolectric/internal/JUnit5MavenDependencyResolver.kt
  • Lines: 112-115
Analysis chain

Log the swallowed exception and verify method usage.

The caught OverlappingFileLockException is swallowed without logging. Consider logging this exception to aid in diagnosing potential issues. Additionally, verify if this method is used elsewhere in the codebase, as it appears unused in the current context.

catch (e: OverlappingFileLockException) {
    // Log the exception for diagnostic purposes
    logger.warn("OverlappingFileLockException encountered: ${e.message}")
    Thread.sleep(100)
}
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify if the `whileLocked(runnable)` method is used elsewhere in the codebase.

# Test: Search for the method usage. Expect: No occurrences if it's unused.
rg --type kotlin 'whileLocked\('

Length of output: 560

Tools
detekt

[warning] 112-112: The caught exception is swallowed. The original exception could be lost.

(detekt.exceptions.SwallowedException)

}
}
}
} catch (e: IOException) {
throw java.lang.IllegalStateException("Couldn't create lock file $lockFile", e)
} finally {
lockFile.delete()
}
}


private companion object {
private const val SPECIAL_CHARACTERS_IN_FILE_NAME_REGEX = """[<>:"\\/|\?\*]"""
}
Expand Down