aboutsummaryrefslogtreecommitdiff
path: root/tests/run/t6488.scala
blob: 55916404424287e2fd72cbbc14439c6ec7b05e74 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import scala.sys.process._
import scala.util.Try
import scala.util.Properties.{ javaHome, javaClassPath }
import java.io.{ File, IOException }
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit._
import java.util.concurrent.atomic._

object Test {
  /*
  // Program that prints "Success" if the command was successfully run then destroyed
  // It will silently pass if the command "/bin/ls" does not exist
  // It will fail due to the uncatchable exception in t6488 race condition,
  // i.e., if any uncaught exceptions on spawned threads are printed.
  def main(args: Array[String]) {
    try Process("/bin/ls").run(ProcessLogger { _ => () }).destroy
    catch { case _ => () }
    println("Success")
  }
  */

  // Show that no uncaught exceptions are thrown on spawned I/O threads
  // when the process is destroyed.  The default handler will print
  // stack traces in the failing case.
  def main(args: Array[String]): Unit = {
    if (args.nonEmpty && args(0) == "data")
      data()
    else
      test()          // args(0) == "jvm"
  }

  // fork the data spewer, wait for input, then destroy the process
  def test(): Unit = {
    val f = new File(javaHome, "bin").listFiles.sorted filter (_.getName startsWith "java") find (_.canExecute) getOrElse {
      // todo signal test runner that test is skipped
      new File("/bin/ls")  // innocuous
    }
    //Process(f.getAbsolutePath).run(ProcessLogger { _ => () }).destroy
    val reading = new CountDownLatch(1)
    val count   = new AtomicInteger
    def counted = count.get
    val command = s"${f.getAbsolutePath} -classpath ${javaClassPath} Test data"
    Try {
      Process(command) run ProcessLogger { (s: String) =>
        //Console println s"[[$s]]"     // java help
        count.getAndIncrement
        reading.countDown
        Thread.`yield`()
      }
    } foreach { (p: Process) =>
      val ok = reading.await(10, SECONDS)
      if (!ok) Console println "Timed out waiting for process output!"
      p.destroy()
    }
    //Console println s"Read count $counted lines"
  }

  // spew something
  def data(): Unit = {
    def filler = "." * 100
    for (i <- 1 to 1000)
      Console println s"Outputting data line $i $filler"
  }
}