From 975bd1dbe9349a6c241fbdc3e763f046962ee829 Mon Sep 17 00:00:00 2001 From: Viktor Klang Date: Sat, 17 Nov 2012 14:26:06 +0100 Subject: [PATCH 1/2] #2717 - Fixing the backoff strategy in PojoSRTestSupport --- .../scala/akka/osgi/PojoSRTestSupport.scala | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/akka-osgi/src/test/scala/akka/osgi/PojoSRTestSupport.scala b/akka-osgi/src/test/scala/akka/osgi/PojoSRTestSupport.scala index 1c70d03d7b..95963bd0f5 100644 --- a/akka-osgi/src/test/scala/akka/osgi/PojoSRTestSupport.scala +++ b/akka-osgi/src/test/scala/akka/osgi/PojoSRTestSupport.scala @@ -10,13 +10,14 @@ import org.apache.commons.io.IOUtils.copy import org.osgi.framework._ import java.net.URL - import java.util.jar.JarInputStream import java.io._ import org.scalatest.{ BeforeAndAfterAll, Suite } import java.util.{ UUID, Date, ServiceLoader, HashMap } import scala.reflect.ClassTag import scala.collection.immutable +import scala.concurrent.duration._ +import scala.annotation.tailrec /** * Trait that provides support for building akka-osgi tests using PojoSR @@ -72,13 +73,18 @@ trait PojoSRTestSupport extends Suite with BeforeAndAfterAll { def awaitReference(serviceType: Class[_]): ServiceReference = awaitReference(serviceType, START_WAIT_TIME) def awaitReference(serviceType: Class[_], wait: Long): ServiceReference = { - val option = Option(context.getServiceReference(serviceType.getName)) - Thread.sleep(wait) //FIXME No sleep please - option match { - case Some(reference) ⇒ reference - case None if (wait > MAX_WAIT_TIME) ⇒ fail("Gave up waiting for service of type %s".format(serviceType)) - case None ⇒ awaitReference(serviceType, wait * 2) + + @tailrec def poll(step: Duration, deadline: Deadline): ServiceReference = context.getServiceReference(serviceType.getName) match { + case null ⇒ + if (deadline.isOverdue()) fail("Gave up waiting for service of type %s".format(serviceType)) + else { + Thread.sleep((step min deadline.timeLeft).toMillis) + poll(step, deadline) + } + case some ⇒ some } + + poll(wait.millis, Deadline.now + MAX_WAIT_TIME.millis) } protected def buildTestBundles(builders: immutable.Seq[BundleDescriptorBuilder]): immutable.Seq[BundleDescriptor] = From a08e5e7c247159785056463c30d40e9405023214 Mon Sep 17 00:00:00 2001 From: Viktor Klang Date: Tue, 20 Nov 2012 15:03:00 +0100 Subject: [PATCH 2/2] #2717 - Changing names of the PojoSR constants to be more Scala-like and switching to FiniteDuration instead of Long --- .../src/test/scala/akka/osgi/PojoSRTestSupport.scala | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/akka-osgi/src/test/scala/akka/osgi/PojoSRTestSupport.scala b/akka-osgi/src/test/scala/akka/osgi/PojoSRTestSupport.scala index 95963bd0f5..d1d77daf1e 100644 --- a/akka-osgi/src/test/scala/akka/osgi/PojoSRTestSupport.scala +++ b/akka-osgi/src/test/scala/akka/osgi/PojoSRTestSupport.scala @@ -24,8 +24,8 @@ import scala.annotation.tailrec */ trait PojoSRTestSupport extends Suite with BeforeAndAfterAll { - val MAX_WAIT_TIME = 12800 - val START_WAIT_TIME = 50 + val MaxWaitDuration = 12800.millis + val SleepyTime = 50.millis /** * All bundles being found on the test classpath are automatically installed and started in the PojoSR runtime. @@ -70,21 +70,21 @@ trait PojoSRTestSupport extends Suite with BeforeAndAfterAll { def serviceForType[T](implicit t: ClassTag[T]): 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 = { @tailrec def poll(step: Duration, deadline: Deadline): ServiceReference = context.getServiceReference(serviceType.getName) match { case null ⇒ if (deadline.isOverdue()) fail("Gave up waiting for service of type %s".format(serviceType)) else { - Thread.sleep((step min deadline.timeLeft).toMillis) + Thread.sleep((step min deadline.timeLeft max Duration.Zero).toMillis) poll(step, deadline) } case some ⇒ some } - poll(wait.millis, Deadline.now + MAX_WAIT_TIME.millis) + poll(wait, Deadline.now + MaxWaitDuration) } protected def buildTestBundles(builders: immutable.Seq[BundleDescriptorBuilder]): immutable.Seq[BundleDescriptor] =