2012-01-19 14:38:44 +00:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package akka.camel
|
|
|
|
|
|
2012-06-25 18:28:38 +02:00
|
|
|
import language.postfixOps
|
|
|
|
|
|
2012-01-19 14:38:44 +00:00
|
|
|
import org.scalatest.matchers.MustMatchers
|
2012-06-29 13:33:20 +02:00
|
|
|
import scala.concurrent.util.duration._
|
2012-01-19 14:38:44 +00:00
|
|
|
import org.apache.camel.ProducerTemplate
|
|
|
|
|
import akka.actor._
|
|
|
|
|
import akka.util.Timeout
|
|
|
|
|
import TestSupport._
|
|
|
|
|
import org.scalatest.WordSpec
|
2012-03-01 17:32:10 +01:00
|
|
|
import akka.testkit.TestLatch
|
2012-06-29 16:06:26 +02:00
|
|
|
import scala.concurrent.Await
|
2012-07-25 20:11:18 +02:00
|
|
|
import java.util.concurrent.TimeoutException
|
2012-01-19 14:38:44 +00:00
|
|
|
|
|
|
|
|
class ActivationIntegrationTest extends WordSpec with MustMatchers with SharedCamelSystem {
|
2012-07-25 20:11:18 +02:00
|
|
|
implicit val timeout = 10 seconds
|
2012-01-19 14:38:44 +00:00
|
|
|
def template: ProducerTemplate = camel.template
|
2012-08-14 13:16:43 +02:00
|
|
|
import system.dispatcher
|
2012-01-19 14:38:44 +00:00
|
|
|
|
|
|
|
|
"ActivationAware must be notified when endpoint is activated" in {
|
2012-07-24 14:26:22 +02:00
|
|
|
val latch = new TestLatch(0)
|
|
|
|
|
val actor = system.actorOf(Props(new TestConsumer("direct:actor-1", latch)))
|
2012-07-25 20:11:18 +02:00
|
|
|
Await.result(camel.activationFutureFor(actor), 10 seconds) must be === actor
|
2012-01-19 14:38:44 +00:00
|
|
|
|
|
|
|
|
template.requestBody("direct:actor-1", "test") must be("received test")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"ActivationAware must be notified when endpoint is de-activated" in {
|
2012-07-24 14:26:22 +02:00
|
|
|
val latch = TestLatch(1)
|
2012-01-19 14:38:44 +00:00
|
|
|
val actor = start(new Consumer {
|
|
|
|
|
def endpointUri = "direct:a3"
|
|
|
|
|
def receive = { case _ ⇒ {} }
|
|
|
|
|
|
2012-03-18 10:46:08 +01:00
|
|
|
override def postStop() {
|
2012-01-19 14:38:44 +00:00
|
|
|
super.postStop()
|
2012-03-01 17:32:10 +01:00
|
|
|
latch.countDown()
|
2012-01-19 14:38:44 +00:00
|
|
|
}
|
|
|
|
|
})
|
2012-07-25 20:11:18 +02:00
|
|
|
Await.result(camel.activationFutureFor(actor), timeout)
|
2012-01-19 14:38:44 +00:00
|
|
|
|
|
|
|
|
system.stop(actor)
|
2012-07-25 20:11:18 +02:00
|
|
|
Await.result(camel.deactivationFutureFor(actor), timeout)
|
2012-07-24 14:26:22 +02:00
|
|
|
Await.ready(latch, 10 second)
|
2012-01-19 14:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"ActivationAware must time out when waiting for endpoint de-activation for too long" in {
|
2012-07-24 14:26:22 +02:00
|
|
|
val latch = new TestLatch(0)
|
|
|
|
|
val actor = start(new TestConsumer("direct:a5", latch))
|
2012-07-25 20:11:18 +02:00
|
|
|
Await.result(camel.activationFutureFor(actor), timeout)
|
|
|
|
|
intercept[TimeoutException] { Await.result(camel.deactivationFutureFor(actor), 1 millis) }
|
2012-01-19 14:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
2012-07-25 20:11:18 +02:00
|
|
|
"activationFutureFor must fail if notification timeout is too short and activation is not complete yet" in {
|
2012-07-24 14:26:22 +02:00
|
|
|
val latch = new TestLatch(1)
|
|
|
|
|
try {
|
|
|
|
|
val actor = system.actorOf(Props(new TestConsumer("direct:actor-4", latch)))
|
2012-07-25 20:11:18 +02:00
|
|
|
intercept[TimeoutException] { Await.result(camel.activationFutureFor(actor), 1 millis) }
|
2012-07-24 14:26:22 +02:00
|
|
|
} finally latch.countDown()
|
2012-01-19 14:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
2012-07-24 14:26:22 +02:00
|
|
|
class TestConsumer(uri: String, latch: TestLatch) extends Consumer {
|
2012-01-19 14:38:44 +00:00
|
|
|
def endpointUri = uri
|
2012-07-24 14:26:22 +02:00
|
|
|
Await.ready(latch, 60 seconds)
|
2012-01-19 14:38:44 +00:00
|
|
|
override def receive = {
|
2012-03-01 17:32:10 +01:00
|
|
|
case msg: CamelMessage ⇒ sender ! "received " + msg.body
|
2012-01-19 14:38:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|