pekko/akka-docs/rst/scala/code/docs/agent/AgentDocSpec.scala

157 lines
3.8 KiB
Scala
Raw Normal View History

2011-12-20 11:07:59 +13:00
/**
2013-01-09 01:47:48 +01:00
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
2011-12-20 11:07:59 +13:00
*/
package docs.agent
2011-12-19 15:46:06 +13:00
import language.postfixOps
2011-12-19 15:46:06 +13:00
import akka.agent.Agent
import scala.concurrent.duration._
2013-01-23 02:06:49 +01:00
import scala.concurrent.{ Await, ExecutionContext }
2011-12-19 15:46:06 +13:00
import akka.testkit._
2013-01-23 02:06:49 +01:00
import scala.concurrent.Future
2011-12-19 15:46:06 +13:00
class AgentDocSpec extends AkkaSpec {
2013-01-23 02:06:49 +01:00
"create" in {
2011-12-19 15:46:06 +13:00
//#create
2013-01-23 02:06:49 +01:00
import scala.concurrent.ExecutionContext.Implicits.global
2011-12-19 15:46:06 +13:00
import akka.agent.Agent
val agent = Agent(5)
//#create
}
2013-01-23 02:06:49 +01:00
"read value" in {
import scala.concurrent.ExecutionContext.Implicits.global
val agent = Agent(0)
2011-12-19 15:46:06 +13:00
2013-01-23 02:06:49 +01:00
{
//#read-apply
val result = agent()
//#read-apply
result must be === 0
}
{
//#read-get
val result = agent.get
//#read-get
result must be === 0
}
2011-12-19 15:46:06 +13:00
2013-01-23 02:06:49 +01:00
{
//#read-future
val future = agent.future
//#read-future
Await.result(future, 5 seconds) must be === 0
}
2011-12-19 15:46:06 +13:00
}
"send and sendOff" in {
2013-01-23 02:06:49 +01:00
val agent = Agent(0)(ExecutionContext.global)
2011-12-19 15:46:06 +13:00
//#send
// send a value, enqueues this change
// of the value of the Agent
2011-12-19 15:46:06 +13:00
agent send 7
// send a function, enqueues this change
// to the value of the Agent
2011-12-19 15:46:06 +13:00
agent send (_ + 1)
agent send (_ * 2)
//#send
2013-01-23 02:06:49 +01:00
def longRunningOrBlockingFunction = (i: Int) i * 1 // Just for the example code
def someExecutionContext() = scala.concurrent.ExecutionContext.Implicits.global // Just for the example code
2011-12-19 15:46:06 +13:00
//#send-off
2013-01-23 02:06:49 +01:00
// the ExecutionContext you want to run the function on
implicit val ec = someExecutionContext()
2011-12-19 15:46:06 +13:00
// sendOff a function
2013-01-23 02:06:49 +01:00
agent sendOff longRunningOrBlockingFunction
2011-12-19 15:46:06 +13:00
//#send-off
2013-01-23 02:06:49 +01:00
Await.result(agent.future, 5 seconds) must be === 16
2011-12-19 15:46:06 +13:00
}
2013-01-23 02:06:49 +01:00
"alter and alterOff" in {
val agent = Agent(0)(ExecutionContext.global)
//#alter
// alter a value
val f1: Future[Int] = agent alter 7
2011-12-19 15:46:06 +13:00
2013-01-23 02:06:49 +01:00
// alter a function
val f2: Future[Int] = agent alter (_ + 1)
val f3: Future[Int] = agent alter (_ * 2)
//#alter
2011-12-19 15:46:06 +13:00
2013-01-23 02:06:49 +01:00
def longRunningOrBlockingFunction = (i: Int) i * 1 // Just for the example code
def someExecutionContext() = ExecutionContext.global // Just for the example code
2011-12-19 15:46:06 +13:00
2013-01-23 02:06:49 +01:00
//#alter-off
// the ExecutionContext you want to run the function on
implicit val ec = someExecutionContext()
// alterOff a function
val f4: Future[Int] = agent alterOff longRunningOrBlockingFunction
//#alter-off
2011-12-19 15:46:06 +13:00
2013-01-23 02:06:49 +01:00
Await.result(f4, 5 seconds) must be === 16
2011-12-19 15:46:06 +13:00
}
"transfer example" in {
//#transfer-example
2013-01-23 02:06:49 +01:00
import scala.concurrent.ExecutionContext.Implicits.global
2011-12-19 15:46:06 +13:00
import akka.agent.Agent
import scala.concurrent.duration._
2011-12-19 15:46:06 +13:00
import scala.concurrent.stm._
def transfer(from: Agent[Int], to: Agent[Int], amount: Int): Boolean = {
atomic { txn
if (from.get < amount) false
else {
from send (_ - amount)
to send (_ + amount)
true
}
}
}
val from = Agent(100)
val to = Agent(20)
val ok = transfer(from, to, 50)
2013-01-23 02:06:49 +01:00
val fromValue = from.future // -> 50
val toValue = to.future // -> 70
2011-12-19 15:46:06 +13:00
//#transfer-example
2013-01-23 02:06:49 +01:00
Await.result(fromValue, 5 seconds) must be === 50
Await.result(toValue, 5 seconds) must be === 70
ok must be === true
2011-12-19 15:46:06 +13:00
}
"monadic example" in {
2013-01-23 02:06:49 +01:00
def println(a: Any) = ()
2011-12-19 15:46:06 +13:00
//#monadic-example
2013-01-23 02:06:49 +01:00
import scala.concurrent.ExecutionContext.Implicits.global
2011-12-19 15:46:06 +13:00
val agent1 = Agent(3)
val agent2 = Agent(5)
// uses foreach
2013-01-23 02:06:49 +01:00
for (value agent1)
println(value)
2011-12-19 15:46:06 +13:00
// uses map
val agent3 = for (value agent1) yield value + 1
// or using map directly
val agent4 = agent1 map (_ + 1)
// uses flatMap
val agent5 = for {
value1 agent1
value2 agent2
} yield value1 + value2
//#monadic-example
agent3() must be === 4
agent4() must be === 4
agent5() must be === 8
}
}