Merging in master, huge work trying to get things to compile, tests not green at this stage
This commit is contained in:
commit
ac5b5de90a
68 changed files with 3759 additions and 2144 deletions
|
|
@ -302,6 +302,26 @@ class ActorDocSpec extends AkkaSpec(Map("akka.loglevel" -> "INFO")) {
|
|||
val actor = system.actorOf(Props(new HotSwapActor), name = "hot")
|
||||
}
|
||||
|
||||
"using Stash" in {
|
||||
//#stash
|
||||
import akka.actor.Stash
|
||||
class ActorWithProtocol extends Actor with Stash {
|
||||
def receive = {
|
||||
case "open" ⇒
|
||||
unstashAll()
|
||||
context.become {
|
||||
case "write" ⇒ // do writing...
|
||||
case "close" ⇒
|
||||
unstashAll()
|
||||
context.unbecome()
|
||||
case msg ⇒ stash()
|
||||
}
|
||||
case msg ⇒ stash()
|
||||
}
|
||||
}
|
||||
//#stash
|
||||
}
|
||||
|
||||
"using watch" in {
|
||||
//#watch
|
||||
import akka.actor.{ Actor, Props, Terminated }
|
||||
|
|
|
|||
|
|
@ -6,8 +6,7 @@ package docs.actor
|
|||
import language.postfixOps
|
||||
|
||||
//#imports
|
||||
import akka.dispatch.{ Promise, Future }
|
||||
import scala.concurrent.Await
|
||||
import scala.concurrent.{ Promise, Future, Await }
|
||||
import scala.concurrent.util.duration._
|
||||
import akka.actor.{ ActorContext, TypedActor, TypedProps }
|
||||
|
||||
|
|
@ -40,7 +39,7 @@ class SquarerImpl(val name: String) extends Squarer {
|
|||
|
||||
def squareDontCare(i: Int): Unit = i * i //Nobody cares :(
|
||||
|
||||
def square(i: Int): Future[Int] = Promise successful i * i
|
||||
def square(i: Int): Future[Int] = Promise.successful(i * i).future
|
||||
|
||||
def squareNowPlease(i: Int): Option[Int] = Some(i * i)
|
||||
|
||||
|
|
@ -56,7 +55,7 @@ trait Foo {
|
|||
|
||||
trait Bar {
|
||||
import TypedActor.dispatcher //So we have an implicit dispatcher for our Promise
|
||||
def doBar(str: String): Future[String] = Promise successful str.toUpperCase
|
||||
def doBar(str: String): Future[String] = Promise.successful(str.toUpperCase).future
|
||||
}
|
||||
|
||||
class FooBar extends Foo with Bar
|
||||
|
|
|
|||
|
|
@ -5,17 +5,13 @@ package docs.future
|
|||
|
||||
import language.postfixOps
|
||||
|
||||
import org.scalatest.{ BeforeAndAfterAll, WordSpec }
|
||||
import org.scalatest.matchers.MustMatchers
|
||||
import akka.testkit._
|
||||
import akka.actor.Actor
|
||||
import akka.actor.Props
|
||||
import akka.actor.{ Actor, Props }
|
||||
import akka.actor.Status.Failure
|
||||
import akka.util.Timeout
|
||||
import scala.concurrent.util.duration._
|
||||
import java.lang.IllegalStateException
|
||||
import akka.dispatch.{ Future, Promise }
|
||||
import scala.concurrent.{ Await, ExecutionContext }
|
||||
import scala.concurrent.{ Await, ExecutionContext, Future, Promise }
|
||||
|
||||
object FutureDocSpec {
|
||||
|
||||
|
|
@ -45,7 +41,7 @@ class FutureDocSpec extends AkkaSpec {
|
|||
"demonstrate usage custom ExecutionContext" in {
|
||||
val yourExecutorServiceGoesHere = java.util.concurrent.Executors.newSingleThreadExecutor()
|
||||
//#diy-execution-context
|
||||
import akka.dispatch.{ ExecutionContext, Promise }
|
||||
import scala.concurrent.{ ExecutionContext, Promise }
|
||||
|
||||
implicit val ec = ExecutionContext.fromExecutorService(yourExecutorServiceGoesHere)
|
||||
|
||||
|
|
@ -120,7 +116,7 @@ class FutureDocSpec extends AkkaSpec {
|
|||
val f1 = Future {
|
||||
"Hello" + "World"
|
||||
}
|
||||
val f2 = Promise.successful(3)
|
||||
val f2 = Promise.successful(3).future
|
||||
val f3 = f1 map { x ⇒
|
||||
f2 map { y ⇒
|
||||
x.length * y
|
||||
|
|
@ -135,7 +131,7 @@ class FutureDocSpec extends AkkaSpec {
|
|||
val f1 = Future {
|
||||
"Hello" + "World"
|
||||
}
|
||||
val f2 = Promise.successful(3)
|
||||
val f2 = Promise.successful(3).future
|
||||
val f3 = f1 flatMap { x ⇒
|
||||
f2 map { y ⇒
|
||||
x.length * y
|
||||
|
|
@ -148,7 +144,7 @@ class FutureDocSpec extends AkkaSpec {
|
|||
|
||||
"demonstrate usage of filter" in {
|
||||
//#filter
|
||||
val future1 = Promise.successful(4)
|
||||
val future1 = Promise.successful(4).future
|
||||
val future2 = future1.filter(_ % 2 == 0)
|
||||
val result = Await.result(future2, 1 second)
|
||||
result must be(4)
|
||||
|
|
@ -293,8 +289,8 @@ class FutureDocSpec extends AkkaSpec {
|
|||
val msg1 = -1
|
||||
//#try-recover
|
||||
val future = akka.pattern.ask(actor, msg1) recoverWith {
|
||||
case e: ArithmeticException ⇒ Promise.successful(0)
|
||||
case foo: IllegalArgumentException ⇒ Promise.failed[Int](new IllegalStateException("All br0ken!"))
|
||||
case e: ArithmeticException ⇒ Promise.successful(0).future
|
||||
case foo: IllegalArgumentException ⇒ Promise.failed[Int](new IllegalStateException("All br0ken!")).future
|
||||
}
|
||||
//#try-recover
|
||||
Await.result(future, 1 second) must be(0)
|
||||
|
|
@ -346,7 +342,7 @@ class FutureDocSpec extends AkkaSpec {
|
|||
Await.result(future, 1 second) must be("foo")
|
||||
}
|
||||
{
|
||||
val future = Promise.failed[String](new IllegalStateException("OHNOES"))
|
||||
val future = Promise.failed[String](new IllegalStateException("OHNOES")).future
|
||||
//#onFailure
|
||||
future onFailure {
|
||||
case ise: IllegalStateException if ise.getMessage == "OHNOES" ⇒
|
||||
|
|
@ -372,10 +368,10 @@ class FutureDocSpec extends AkkaSpec {
|
|||
|
||||
"demonstrate usage of Promise.success & Promise.failed" in {
|
||||
//#successful
|
||||
val future = Promise.successful("Yay!")
|
||||
val future = Promise.successful("Yay!").future
|
||||
//#successful
|
||||
//#failed
|
||||
val otherFuture = Promise.failed[String](new IllegalArgumentException("Bang!"))
|
||||
val otherFuture = Promise.failed[String](new IllegalArgumentException("Bang!")).future
|
||||
//#failed
|
||||
Await.result(future, 1 second) must be("Yay!")
|
||||
intercept[IllegalArgumentException] { Await.result(otherFuture, 1 second) }
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ import language.postfixOps
|
|||
import akka.testkit.TestProbe
|
||||
import scala.concurrent.util.duration._
|
||||
import akka.actor._
|
||||
import scala.concurrent.Futures
|
||||
import scala.concurrent.Future
|
||||
|
||||
//#imports-test-probe
|
||||
|
||||
|
|
@ -127,7 +127,10 @@ class TestkitDocSpec extends AkkaSpec with DefaultTimeout with ImplicitSender {
|
|||
|
||||
val actorRef = TestActorRef(new MyActor)
|
||||
// hypothetical message stimulating a '42' answer
|
||||
val result = Await.result((actorRef ? Say42), 5 seconds).asInstanceOf[Int]
|
||||
val future = actorRef ? Say42
|
||||
val result = future.value.get match {
|
||||
case Right(x: Int) ⇒ x
|
||||
}
|
||||
result must be(42)
|
||||
//#test-behavior
|
||||
}
|
||||
|
|
@ -148,7 +151,7 @@ class TestkitDocSpec extends AkkaSpec with DefaultTimeout with ImplicitSender {
|
|||
|
||||
val actorRef = TestActorRef(new Actor {
|
||||
def receive = {
|
||||
case boom ⇒ throw new IllegalArgumentException("boom")
|
||||
case "hello" ⇒ throw new IllegalArgumentException("boom")
|
||||
}
|
||||
})
|
||||
intercept[IllegalArgumentException] { actorRef.receive("hello") }
|
||||
|
|
@ -274,4 +277,15 @@ class TestkitDocSpec extends AkkaSpec with DefaultTimeout with ImplicitSender {
|
|||
//#test-kit-base
|
||||
}
|
||||
|
||||
"demonstrate within() nesting" in {
|
||||
intercept[AssertionError] {
|
||||
//#test-within-probe
|
||||
val probe = TestProbe()
|
||||
within(1 second) {
|
||||
probe.expectMsg("hello")
|
||||
}
|
||||
//#test-within-probe
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue