Skip to content

Commit

Permalink
#2717 - Fixing the backoff strategy in PojoSRTestSupport
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorklang committed Nov 17, 2012
1 parent 1d768fa commit 975bd1d
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions akka-osgi/src/test/scala/akka/osgi/PojoSRTestSupport.scala
Expand Up @@ -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
Expand Down Expand Up @@ -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] =
Expand Down

0 comments on commit 975bd1d

Please sign in to comment.