diff --git a/runtime/js/src/main/scala/scoverage/Platform.scala b/runtime/js/src/main/scala/scoverage/Platform.scala index 70f7bb07..9b326b98 100644 --- a/runtime/js/src/main/scala/scoverage/Platform.scala +++ b/runtime/js/src/main/scala/scoverage/Platform.scala @@ -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 = () + } diff --git a/runtime/jvm/src/main/scala/scoverage/Platform.scala b/runtime/jvm/src/main/scala/scoverage/Platform.scala index f8e66d43..63e15924 100644 --- a/runtime/jvm/src/main/scala/scoverage/Platform.scala +++ b/runtime/jvm/src/main/scala/scoverage/Platform.scala @@ -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] @@ -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 => + () + } + } + } diff --git a/runtime/jvm/src/test/scala/scoverage/InvokerConcurrencyTest.scala b/runtime/jvm/src/test/scala/scoverage/InvokerConcurrencyTest.scala index d2f3df2a..0d9ac8ef 100644 --- a/runtime/jvm/src/test/scala/scoverage/InvokerConcurrencyTest.scala +++ b/runtime/jvm/src/test/scala/scoverage/InvokerConcurrencyTest.scala @@ -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() diff --git a/runtime/native/src/main/scala/scoverage/Platform.scala b/runtime/native/src/main/scala/scoverage/Platform.scala index fa3902d3..da640f2a 100644 --- a/runtime/native/src/main/scala/scoverage/Platform.scala +++ b/runtime/native/src/main/scala/scoverage/Platform.scala @@ -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] @@ -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 => + () + } + } + } diff --git a/runtime/shared/src/main/scala/scoverage/Invoker.scala b/runtime/shared/src/main/scala/scoverage/Invoker.scala index f7615137..36f83988 100644 --- a/runtime/shared/src/main/scala/scoverage/Invoker.scala +++ b/runtime/shared/src/main/scala/scoverage/Invoker.scala @@ -2,6 +2,7 @@ package scoverage import scala.collection.Set import scala.collection.mutable +import scala.util.control.NonFatal import scoverage.Platform._ @@ -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..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. @@ -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 @@ -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) @@ -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) )