~ubuntu-branches/debian/sid/scala/sid

« back to all changes in this revision

Viewing changes to src/partest-alternative/scala/tools/partest/Alarms.scala

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg, Mehdi Dogguy, Lucas Satabin, Frank S. Thomas, Emmanuel Bourg
  • Date: 2015-06-05 23:52:59 UTC
  • mfrom: (1.2.11)
  • Revision ID: package-import@ubuntu.com-20150605235259-wk00vgk83dh8o19g
Tags: 2.10.5-1
* Team upload.

[ Mehdi Dogguy ]
* New upstream release (Closes: #744278).

[ Lucas Satabin ]
* Update patches
* Update the clean target
* Update paths of elements to install
* Update watch file

[ Frank S. Thomas ]
* Remove myself from Uploaders.

[ Emmanuel Bourg ]
* The package has been adopted by the Java Team (Closes: #754935)
* Patched the build to avoid downloading libraries from the Internet
* Replaced the minified JavaScript files with unobfuscated ones
* No longer build scala-partest.jar until diffutils is packaged or replaced
* debian/watch: Fixed the versions matched (x.y.z instead of x.y.z..z)
* debian/rules:
  - Added the missing get-orig-source target (Closes: #724704)
  - Improved the clean target
* debian/control:
  - Build depend on scala (>= 2.10) and bnd
  - Use canonical URLs for the Vcs-* fields
  - Standards-Version updated to 3.9.6 (no changes)
* Switch to debhelper level 9

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* NEST (New Scala Test)
2
 
 * Copyright 2007-2011 LAMP/EPFL
3
 
 * @author Paul Phillips
4
 
 */
5
 
 
6
 
package scala.tools
7
 
package partest
8
 
 
9
 
import java.util.{ Timer, TimerTask }
10
 
 
11
 
trait Alarms {
12
 
  self: Universe =>
13
 
 
14
 
  def interruptMeIn[T](debugMsg: String, seconds: Int)(body: => T): Option[T] = {
15
 
    val thisThread  = currentThread
16
 
    val alarm       = new SimpleAlarm(seconds * 1000) set thisThread.interrupt()
17
 
    debug("interruptMeIn(%d) '%s'".format(seconds, debugMsg))
18
 
 
19
 
    try     { Some(body) }
20
 
    catch   { case _: InterruptedException => debug("Received interrupted exception.") ; None }
21
 
    finally { debug("Cancelling interruptMeIn '%s'" format debugMsg) ; alarm.cancel() ; Thread.interrupted() }
22
 
  }
23
 
 
24
 
  case class AlarmerAction(secs: Int, action: () => Unit) extends Runnable {
25
 
    override def run() = action()
26
 
  }
27
 
 
28
 
  /** Set any number of alarms up with tuples of the form:
29
 
   *    seconds to alarm -> Function0[Unit] to execute
30
 
   */
31
 
  class Alarmer(alarms: AlarmerAction*) {
32
 
    import java.util.concurrent._
33
 
 
34
 
    val exec = Executors.newSingleThreadScheduledExecutor()
35
 
    alarms foreach (x => exec.schedule(x, x.secs, TimeUnit.SECONDS))
36
 
    exec.shutdown()
37
 
 
38
 
    def cancelAll() = exec.shutdownNow()
39
 
  }
40
 
 
41
 
  class SimpleAlarm(timeout: Long) {
42
 
    private val alarm = new Timer
43
 
 
44
 
    /** Start a timer, running the given body if it goes off.
45
 
     */
46
 
    def set(body: => Unit) = returning(new TimerTask { def run() = body })(alarm.schedule(_, timeout))
47
 
 
48
 
    /** Cancel the timer.
49
 
     */
50
 
    def cancel() = alarm.cancel()
51
 
  }
52
 
 
53
 
  trait TestAlarms {
54
 
    test: TestEntity =>
55
 
 
56
 
    private def warning1 = AlarmerAction(testWarning, () => warning(
57
 
      """|I've been waiting %s seconds for this to complete:
58
 
         |  %s
59
 
         |It may be stuck, or if not, it should be broken into smaller tests.
60
 
         |""".stripMargin.format(testWarning, test))
61
 
    )
62
 
    private def warning2 = AlarmerAction(testWarning * 2, () => warning(
63
 
      """|Now I've been waiting %s seconds for this to complete:
64
 
         |  %s
65
 
         |If partest seems hung it would be a good place to look.
66
 
         |""".stripMargin.format(testWarning * 2, test))
67
 
    )
68
 
 
69
 
    def startAlarms(onTimeout: => Unit) =
70
 
      if (isNoAlarms) new Alarmer() // for alarm debugging
71
 
      else new Alarmer(Seq(warning1, warning2, AlarmerAction(testTimeout, () => onTimeout)): _*)
72
 
  }
73
 
 
74
 
  // Thread.setDefaultUncaughtExceptionHandler(new UncaughtException)
75
 
  // class UncaughtException extends Thread.UncaughtExceptionHandler {
76
 
  //   def uncaughtException(t: Thread, e: Throwable) {
77
 
  //     Console.println("Uncaught in %s: %s".format(t, e))
78
 
  //   }
79
 
  // }
80
 
  //
81
 
  // lazy val logger = File("/tmp/partest.log").bufferedWriter()
82
 
  // def flog(msg: String) = logger synchronized {
83
 
  //   logger write (msg + "\n")
84
 
  //   logger.flush()
85
 
  // }
86
 
}