pekko/akka-docs/rst/java/code/docs/agent/AgentDocTest.java

116 lines
2.9 KiB
Java
Raw Normal View History

2011-12-20 11:07:59 +13:00
/**
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
2011-12-20 11:07:59 +13:00
*/
package docs.agent;
import static org.junit.Assert.*;
import org.junit.Test;
2013-01-23 02:06:49 +01:00
import scala.concurrent.Await;
import scala.concurrent.duration.Duration;
//#import-agent
2013-01-23 02:06:49 +01:00
import scala.concurrent.ExecutionContext;
import akka.agent.Agent;
import akka.dispatch.ExecutionContexts;
//#import-agent
//#import-function
2013-01-23 02:06:49 +01:00
import akka.dispatch.Mapper;
//#import-function
2013-01-23 02:06:49 +01:00
//#import-future
import scala.concurrent.Future;
//#import-future
public class AgentDocTest {
2013-01-23 02:06:49 +01:00
private static ExecutionContext ec = ExecutionContexts.global();
@Test
2013-01-23 02:06:49 +01:00
public void createAndRead() throws Exception {
//#create
ExecutionContext ec = ExecutionContexts.global();
Agent<Integer> agent = Agent.create(5, ec);
//#create
2013-01-23 02:06:49 +01:00
//#read-get
Integer result = agent.get();
//#read-get
2013-01-23 02:06:49 +01:00
//#read-future
Future<Integer> future = agent.future();
//#read-future
assertEquals(result, new Integer(5));
assertEquals(Await.result(future, Duration.create(5,"s")), new Integer(5));
}
@Test
2013-01-23 02:06:49 +01:00
public void sendAndSendOffAndReadAwait() throws Exception {
Agent<Integer> agent = Agent.create(5, ec);
//#send
// send a value, enqueues this change
// of the value of the Agent
agent.send(7);
// send a Mapper, enqueues this change
// to the value of the Agent
2013-01-23 02:06:49 +01:00
agent.send(new Mapper<Integer, Integer>() {
public Integer apply(Integer i) {
return i * 2;
}
});
//#send
2013-01-23 02:06:49 +01:00
Mapper<Integer, Integer> longRunningOrBlockingFunction = new Mapper<Integer, Integer>() {
public Integer apply(Integer i) {
return i * 1;
}
};
2013-01-23 02:06:49 +01:00
ExecutionContext theExecutionContextToExecuteItIn = ec;
//#send-off
// sendOff a function
2013-01-23 02:06:49 +01:00
agent.sendOff(longRunningOrBlockingFunction,
theExecutionContextToExecuteItIn);
//#send-off
2013-01-23 02:06:49 +01:00
assertEquals(Await.result(agent.future(), Duration.create(5,"s")), new Integer(14));
}
2013-01-23 02:06:49 +01:00
@Test
public void alterAndAlterOff() throws Exception {
Agent<Integer> agent = Agent.create(5, ec);
2013-01-23 02:06:49 +01:00
//#alter
// alter a value
Future<Integer> f1 = agent.alter(7);
2013-01-23 02:06:49 +01:00
// alter a function (Mapper)
Future<Integer> f2 = agent.alter(new Mapper<Integer, Integer>() {
public Integer apply(Integer i) {
return i * 2;
}
});
//#alter
2013-01-23 02:06:49 +01:00
Mapper<Integer, Integer> longRunningOrBlockingFunction = new Mapper<Integer, Integer>() {
public Integer apply(Integer i) {
return i * 1;
}
};
2013-01-23 02:06:49 +01:00
ExecutionContext theExecutionContextToExecuteItIn = ec;
//#alter-off
// alterOff a function (Mapper)
Future<Integer> f3 = agent.alterOff(longRunningOrBlockingFunction,
theExecutionContextToExecuteItIn);
//#alter-off
2013-01-23 02:06:49 +01:00
assertEquals(Await.result(f3, Duration.create(5,"s")), new Integer(14));
}
2013-01-09 01:47:48 +01:00
}