Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions runtime/js/src/main/scala/scoverage/Platform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ object Platform {
new java.util.UUID(msb, lsb)
}

// Scala.js does not run JVM shutdown hooks. Measurement files are still
// written from `invoked`, and `dumpRecordedIds` can be called directly.
def onShutdown(hook: () => Unit): Unit = ()

}
18 changes: 18 additions & 0 deletions runtime/jvm/src/main/scala/scoverage/Platform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import java.io.{FileWriter => SupportFileWriter}

import scala.collection.concurrent.TrieMap
import scala.io.{Source => SupportSource}
import scala.util.control.NonFatal

object Platform {
type ThreadSafeMap[A, B] = TrieMap[A, B]
Expand All @@ -19,4 +20,21 @@ object Platform {

def insecureRandomUUID() = java.util.UUID.randomUUID()

def onShutdown(hook: () => Unit): Unit = {
val thread = new Thread(new Runnable {
override def run(): Unit =
try hook()
catch { case NonFatal(_) => () }
})
thread.setName("scoverage-measurement-dump")
try Runtime.getRuntime.addShutdownHook(thread)
catch {
case _: IllegalStateException =>
try hook()
catch { case NonFatal(_) => () }
case _: SecurityException =>
()
}
}

}
27 changes: 27 additions & 0 deletions runtime/jvm/src/test/scala/scoverage/InvokerConcurrencyTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,33 @@ class InvokerConcurrencyTest extends FunSuite {
assertEquals(idsFromFile, testIds)
}

test("shutdown-snapshot-keeps-ids-recorded-from-many-threads") {
val dir = new File("target/invoker-test-snapshot.measurement")
if (dir.isDirectory()) dir.listFiles().foreach(_.delete())
dir.mkdirs()

val testIds: Set[Int] = (1 to 1000).toSet
val futures: Set[Future[Unit]] = testIds.map { i: Int =>
Future {
Invoker.invoked(i, dir.getAbsolutePath())
}
}
futures.foreach(Await.result(_, 3.second))

Invoker.findMeasurementFiles(dir).foreach(_.delete())
Invoker.dumpRecordedIds()

val shutdownFiles =
Invoker
.findMeasurementFiles(dir)
.filter(_.getName().endsWith(".shutdown"))
assertEquals(shutdownFiles.length, 1)
assertEquals(Invoker.invoked(shutdownFiles.toIndexedSeq), testIds)

dir.listFiles().foreach(_.delete())
dir.delete()
}

override def afterAll(): Unit = {
deleteMeasurementFiles()
measurementDir.delete()
Expand Down
18 changes: 18 additions & 0 deletions runtime/native/src/main/scala/scoverage/Platform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import java.io.{FileWriter => SupportFileWriter}

import scala.collection.mutable.HashMap
import scala.io.{Source => SupportSource}
import scala.util.control.NonFatal

object Platform {
type ThreadSafeMap[A, B] = HashMap[A, B]
Expand All @@ -28,4 +29,21 @@ object Platform {
new java.util.UUID(msb, lsb)
}

def onShutdown(hook: () => Unit): Unit = {
val thread = new Thread(new Runnable {
override def run(): Unit =
try hook()
catch { case NonFatal(_) => () }
})
thread.setName("scoverage-measurement-dump")
try Runtime.getRuntime.addShutdownHook(thread)
catch {
case _: IllegalStateException =>
try hook()
catch { case NonFatal(_) => () }
case _: SecurityException =>
()
}
}

}
91 changes: 77 additions & 14 deletions runtime/shared/src/main/scala/scoverage/Invoker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scoverage

import scala.collection.Set
import scala.collection.mutable
import scala.util.control.NonFatal

import scoverage.Platform._

Expand All @@ -11,19 +12,28 @@ object Invoker {
private val runtimeUUID = Platform.insecureRandomUUID()

private val MeasurementsPrefix = "scoverage.measurements."
private val threadFiles = new ThreadLocal[mutable.HashMap[String, FileWriter]]
private val threadFiles =
new ThreadLocal[mutable.HashMap[String, FileWriter]]

// For each data directory we maintain a thread-safe set tracking the ids that we've already
// seen and recorded. We're using a map as a set, so we only care about its keys and can ignore
// its values.
// seen and recorded. We're using a map as a set. The value is the recorded test name, or ""
// when test names are not reported.
private val dataDirToIds =
ThreadSafeMap.empty[String, ThreadSafeMap[Int, Any]]

@volatile private var shutdownHookInstalled = false

/** We record that the given id has been invoked by appending its id to the coverage
* data file.
*
* This will happen concurrently on as many threads as the application is using,
* so we use one file per thread, named for the thread id.
* Because each id is written at most once, a lost per-thread write cannot be
* repaired by a later call.
*
* The in-memory id set is therefore also written to
* `scoverage.measurements.<uuid>.shutdown` from a shutdown hook. The reporter unions
* every measurement file, and duplicate ids do not change coverage.
*
* This method is not thread-safe if the threads are in different JVMs, because
* the thread IDs may collide.
Expand All @@ -38,6 +48,7 @@ object Invoker {
dataDir: String,
reportTestName: Boolean = false
): Unit = {
ensureShutdownHook()
// [sam] we can do this simple check to save writing out to a file.
// This won't work across JVMs but since there's no harm in writing out the same id multiple
// times since for coverage we only care about 1 or more, (it just slows things down to
Expand All @@ -55,28 +66,68 @@ object Invoker {
if (!ids.contains(id)) {
// Each thread writes to a separate measurement file, to reduce contention
// and because file appends via FileWriter are not atomic on Windows.
var files = threadFiles.get()
if (files == null) {
files = mutable.HashMap.empty[String, FileWriter]
threadFiles.set(files)
}
val writer = files.getOrElseUpdate(
dataDir,
new FileWriter(measurementFile(dataDir), true)
)
val writer = writerFor(dataDir)
val testName = if (reportTestName) getCallingScalaTest else ""

if (reportTestName)
writer
.append(Integer.toString(id))
.append(" ")
.append(getCallingScalaTest)
.append(testName)
.append("\n")
.flush()
else writer.append(Integer.toString(id)).append("\n").flush()
ids.put(id, ())
ids.put(id, testName)
}
}

private def ensureShutdownHook(): Unit =
if (!shutdownHookInstalled) {
synchronized {
if (!shutdownHookInstalled) {
Platform.onShutdown(() => dumpRecordedIds())
shutdownHookInstalled = true
}
}
}

/** Write every id recorded in memory into a measurement file.
*
* Per-thread files are an optimization. This snapshot is what the reporter
* can trust after the instrumented JVM exits: forked tests (the usual sbt
* `coverage` / `test` / `coverageReport` sequence) read measurement files
* only once that process has shut down.
*/
private[scoverage] def dumpRecordedIds(): Unit =
dataDirToIds.foreach { case (dataDir, ids) =>
try {
val dir = new File(dataDir)
if (dir.isDirectory() && ids.nonEmpty) {
val content = new mutable.StringBuilder
ids.foreach { case (id, rawName) =>
content.append(Integer.toString(id))
rawName match {
case name: String if name.nonEmpty =>
content.append(" ").append(name)
case _ =>
}
content.append('\n')
}
val writer = new FileWriter(
new File(
dataDir,
MeasurementsPrefix + runtimeUUID + ".shutdown"
),
true
)
try writer.append(content).flush()
finally writer.close()
}
} catch {
case NonFatal(_) => ()
}
}

def getCallingScalaTest: String =
Thread.currentThread.getStackTrace
.map(_.getClassName.toLowerCase)
Expand All @@ -93,6 +144,18 @@ object Invoker {
MeasurementsPrefix + runtimeUUID + "." + Thread.currentThread.getId
)

private def writerFor(dataDir: String): FileWriter = {
var files = threadFiles.get()
if (files == null) {
files = mutable.HashMap.empty[String, FileWriter]
threadFiles.set(files)
}
files.getOrElseUpdate(
dataDir,
new FileWriter(measurementFile(dataDir), true)
)
}

def findMeasurementFiles(dataDir: String): Array[File] = findMeasurementFiles(
new File(dataDir)
)
Expand Down
Loading