diff options
author | Aleksandar Pokopec <aleksandar.prokopec@epfl.ch> | 2010-10-12 16:17:40 +0000 |
---|---|---|
committer | Aleksandar Pokopec <aleksandar.prokopec@epfl.ch> | 2010-10-12 16:17:40 +0000 |
commit | 38d450043069c9c2607393135f110bcf215790ca (patch) | |
tree | b4616dac07bd85be91ae6b6d89ce4bdea7de39f4 | |
parent | cf11854cf0f21b57634d8e7ec68ac23539ed4fb6 (diff) | |
download | scala-38d450043069c9c2607393135f110bcf215790ca.tar.gz scala-38d450043069c9c2607393135f110bcf215790ca.tar.bz2 scala-38d450043069c9c2607393135f110bcf215790ca.zip |
One more partest fix where output was redirecte...
One more partest fix where output was redirected using a global
variable, causing errors with multiple actors. Review by plocinic
-rw-r--r-- | src/partest/scala/tools/partest/nest/DirectRunner.scala | 11 | ||||
-rw-r--r-- | src/partest/scala/tools/partest/nest/Worker.scala | 74 |
2 files changed, 62 insertions, 23 deletions
diff --git a/src/partest/scala/tools/partest/nest/DirectRunner.scala b/src/partest/scala/tools/partest/nest/DirectRunner.scala index 6875b3794e..40f397e86b 100644 --- a/src/partest/scala/tools/partest/nest/DirectRunner.scala +++ b/src/partest/scala/tools/partest/nest/DirectRunner.scala @@ -18,6 +18,10 @@ import scala.tools.nsc.io.Directory import scala.actors.Actor._ import scala.actors.TIMEOUT + +case class TestRunParams(val scalaCheckParentClassLoader: ScalaClassLoader) + + trait DirectRunner { def fileManager: FileManager @@ -39,10 +43,13 @@ trait DirectRunner { val consFM = new ConsoleFileManager import consFM.{ latestCompFile, latestLibFile, latestPartestFile } val scalacheckURL = PathSettings.scalaCheck.toURL - val scalaCheckParentClassLoader = ScalaClassLoader.fromURLs(List(scalacheckURL, latestCompFile.toURI.toURL, latestLibFile.toURI.toURL, latestPartestFile.toURI.toURL)) + val scalaCheckParentClassLoader = ScalaClassLoader.fromURLs( + List(scalacheckURL, latestCompFile.toURI.toURL, latestLibFile.toURI.toURL, latestPartestFile.toURI.toURL) + ) + Output.init val workers = for (i <- List.range(0, numActors)) yield { val toTest = kindFiles.slice(i*testsEach, (i+1)*testsEach) - val worker = new Worker(fileManager, scalaCheckParentClassLoader) + val worker = new Worker(fileManager, TestRunParams(scalaCheckParentClassLoader)) worker.start() if (i == last) worker ! RunTests(kind, (kindFiles splitAt (last*testsEach))._2) diff --git a/src/partest/scala/tools/partest/nest/Worker.scala b/src/partest/scala/tools/partest/nest/Worker.scala index 637e5160ee..fba24394a5 100644 --- a/src/partest/scala/tools/partest/nest/Worker.scala +++ b/src/partest/scala/tools/partest/nest/Worker.scala @@ -54,7 +54,55 @@ class ScalaCheckFileManager(val origmanager: FileManager) extends FileManager { var LATEST_LIB: String = origmanager.LATEST_LIB } -class Worker(val fileManager: FileManager, scalaCheckParentClassLoader: ScalaClassLoader) extends Actor { +object Output { + def init { + System.setOut(outRedirect) + System.setErr(errRedirect) + } + + import scala.util.DynamicVariable + private def out = java.lang.System.out + private def err = java.lang.System.err + private val redirVar = new DynamicVariable[Option[PrintStream]](None) + + class Redirecter(stream: PrintStream) extends PrintStream(new OutputStream { + private def withStream(f: PrintStream => Unit) = { + if (redirVar.value != None) f(redirVar.value.get) + else f(stream) + } + def write(b: Int) = withStream(_.write(b)) + override def write(b: Array[Byte]) = withStream(_.write(b)) + override def write(b: Array[Byte], off: Int, len: Int) = withStream(_.write(b, off, len)) + override def flush = withStream(_.flush) + override def close = withStream(_.close) + }) + + object outRedirect extends Redirecter(out) + + object errRedirect extends Redirecter(err) + + // this supports thread-safe nested output redirects + def withRedirected(newstream: PrintStream)(func: => Unit) { + // note down old redirect destination + // this may be None in which case outRedirect and errRedirect print to stdout and stderr + val oldred = redirVar.value + + // set new redirecter + // this one will redirect both out and err to newstream + redirVar.value = Some(newstream) + + try { + func + } finally { + // revert to old redirect + // this may be None, which makes outRedirect and errRedirect choose standard outputs again + redirVar.value = oldred + } + } +} + + +class Worker(val fileManager: FileManager, params: TestRunParams) extends Actor { import fileManager._ val scalaCheckFileManager = new ScalaCheckFileManager(fileManager) @@ -478,10 +526,10 @@ class Worker(val fileManager: FileManager, scalaCheckParentClassLoader: ScalaCla val logWriter = new PrintStream(new FileOutputStream(logFile)) - withOutputRedirected(logWriter) { + Output.withRedirected(logWriter) { // this classloader is test specific // its parent contains library classes and others - val classloader = ScalaClassLoader.fromURLs(List(outURL), scalaCheckParentClassLoader) + val classloader = ScalaClassLoader.fromURLs(List(outURL), params.scalaCheckParentClassLoader) classloader.run("Test", Nil) } @@ -638,7 +686,7 @@ class Worker(val fileManager: FileManager, scalaCheckParentClassLoader: ScalaCla } } - withOutputRedirected(logWriter) { + Output.withRedirected(logWriter) { loop() testReader.close() } @@ -751,7 +799,7 @@ class Worker(val fileManager: FileManager, scalaCheckParentClassLoader: ScalaCla } } - withOutputRedirected(logWriter) { + Output.withRedirected(logWriter) { loop(resCompile) resReader.close() } @@ -1054,22 +1102,6 @@ class Worker(val fileManager: FileManager, scalaCheckParentClassLoader: ScalaCla } } - private def withOutputRedirected(out: PrintStream)(func: => Unit) { - val oldStdOut = System.out - val oldStdErr = System.err - - try { - System.setOut(out) - System.setErr(out) - func - out.flush() - out.close() - } finally { - System.setOut(oldStdOut) - System.setErr(oldStdErr) - } - } - private def filesToSet(pre: String, fs: List[String]): Set[AbstractFile] = fs flatMap (s => Option(AbstractFile getFile (pre + s))) toSet |