Merge pull request #878 from akka/wip-2717-√
#2717 - Fixing the backoff strategy in PojoSRTestSupport
This commit is contained in:
commit
3c946cc8f0
1 changed files with 17 additions and 11 deletions
|
|
@ -10,21 +10,22 @@ import org.apache.commons.io.IOUtils.copy
|
||||||
|
|
||||||
import org.osgi.framework._
|
import org.osgi.framework._
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
|
|
||||||
import java.util.jar.JarInputStream
|
import java.util.jar.JarInputStream
|
||||||
import java.io._
|
import java.io._
|
||||||
import org.scalatest.{ BeforeAndAfterAll, Suite }
|
import org.scalatest.{ BeforeAndAfterAll, Suite }
|
||||||
import java.util.{ UUID, Date, ServiceLoader, HashMap }
|
import java.util.{ UUID, Date, ServiceLoader, HashMap }
|
||||||
import scala.reflect.ClassTag
|
import scala.reflect.ClassTag
|
||||||
import scala.collection.immutable
|
import scala.collection.immutable
|
||||||
|
import scala.concurrent.duration._
|
||||||
|
import scala.annotation.tailrec
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Trait that provides support for building akka-osgi tests using PojoSR
|
* Trait that provides support for building akka-osgi tests using PojoSR
|
||||||
*/
|
*/
|
||||||
trait PojoSRTestSupport extends Suite with BeforeAndAfterAll {
|
trait PojoSRTestSupport extends Suite with BeforeAndAfterAll {
|
||||||
|
|
||||||
val MAX_WAIT_TIME = 12800
|
val MaxWaitDuration = 12800.millis
|
||||||
val START_WAIT_TIME = 50
|
val SleepyTime = 50.millis
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* All bundles being found on the test classpath are automatically installed and started in the PojoSR runtime.
|
* All bundles being found on the test classpath are automatically installed and started in the PojoSR runtime.
|
||||||
|
|
@ -69,16 +70,21 @@ trait PojoSRTestSupport extends Suite with BeforeAndAfterAll {
|
||||||
def serviceForType[T](implicit t: ClassTag[T]): T =
|
def serviceForType[T](implicit t: ClassTag[T]): T =
|
||||||
context.getService(awaitReference(t.runtimeClass)).asInstanceOf[T]
|
context.getService(awaitReference(t.runtimeClass)).asInstanceOf[T]
|
||||||
|
|
||||||
def awaitReference(serviceType: Class[_]): ServiceReference = awaitReference(serviceType, START_WAIT_TIME)
|
def awaitReference(serviceType: Class[_]): ServiceReference = awaitReference(serviceType, SleepyTime)
|
||||||
|
|
||||||
def awaitReference(serviceType: Class[_], wait: Long): ServiceReference = {
|
def awaitReference(serviceType: Class[_], wait: FiniteDuration): ServiceReference = {
|
||||||
val option = Option(context.getServiceReference(serviceType.getName))
|
|
||||||
Thread.sleep(wait) //FIXME No sleep please
|
@tailrec def poll(step: Duration, deadline: Deadline): ServiceReference = context.getServiceReference(serviceType.getName) match {
|
||||||
option match {
|
case null ⇒
|
||||||
case Some(reference) ⇒ reference
|
if (deadline.isOverdue()) fail("Gave up waiting for service of type %s".format(serviceType))
|
||||||
case None if (wait > MAX_WAIT_TIME) ⇒ fail("Gave up waiting for service of type %s".format(serviceType))
|
else {
|
||||||
case None ⇒ awaitReference(serviceType, wait * 2)
|
Thread.sleep((step min deadline.timeLeft max Duration.Zero).toMillis)
|
||||||
|
poll(step, deadline)
|
||||||
}
|
}
|
||||||
|
case some ⇒ some
|
||||||
|
}
|
||||||
|
|
||||||
|
poll(wait, Deadline.now + MaxWaitDuration)
|
||||||
}
|
}
|
||||||
|
|
||||||
protected def buildTestBundles(builders: immutable.Seq[BundleDescriptorBuilder]): immutable.Seq[BundleDescriptor] =
|
protected def buildTestBundles(builders: immutable.Seq[BundleDescriptorBuilder]): immutable.Seq[BundleDescriptor] =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue