Skip to content

Commit

Permalink
Make better sync timeboxed calls
Browse files Browse the repository at this point in the history
  • Loading branch information
wpopielarski committed Dec 21, 2015
1 parent 86011e7 commit 1878228
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
package org.scalaide.debug.internal.model

import com.sun.jdi.event.ClassPrepareEvent
import scala.concurrent.Await
import scala.concurrent.Future
import scala.util.Try

trait ClassPrepareListener {
import scala.concurrent.duration._
val Timeout = 500 millis
type Timeouted = Boolean

def notify(event: ClassPrepareEvent): Timeouted = {
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.TimeoutException

Try {
Await.ready(Future { consume(event) }, Timeout)
}.map { _ =>
false
}.recover {
case e: TimeoutException => true
}.get
}
def notify(event: ClassPrepareEvent): Boolean = SyncCall.ready(consume(event))

protected def consume(event: ClassPrepareEvent): Unit
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import scala.concurrent.Future
import org.scalaide.logging.HasLogger

object SyncCall extends HasLogger {
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
val Timeout = 500 millis
type TimeoutOccurred = Boolean

def apply[T](f: => T): Option[T] = {
import scala.concurrent.ExecutionContext.Implicits.global
def result[T](f: => T): Option[T] = {
import scala.concurrent.TimeoutException

Try {
Expand All @@ -19,8 +20,20 @@ object SyncCall extends HasLogger {
Option.apply
}.recover {
case e: TimeoutException =>
logger.info("TIMEOUT waiting for debug cache actor in getLoadedNestedTypes")
logger.info("TIMEOUT waiting while 'f' called")
None
}.get
}

def ready(f: => Unit): TimeoutOccurred = {
import scala.concurrent.TimeoutException

Try {
Await.ready(Future { f }, Timeout)
}.map { _ =>
false
}.recover {
case e: TimeoutException => true
}.get
}
}

0 comments on commit 1878228

Please sign in to comment.