Skip to content

Commit

Permalink
Add convenience method to start timer without key (akka#27875)
Browse files Browse the repository at this point in the history
* Add convenience method to start timer without key

It is probably common that there is no need to allow different timers
that send the same message, and this makes that more convenient to write.

Updated one method to gather feedback, if we like the change I can apply
it to the others as well.

* Add alternative to all typed timer API's

Update java/scaladoc, update tests

Not updated classic actors and FSM API's
  • Loading branch information
raboof authored and navaro1 committed Dec 17, 2019
1 parent 1db2c8e commit 3f3d877
Show file tree
Hide file tree
Showing 21 changed files with 253 additions and 116 deletions.
Expand Up @@ -30,7 +30,11 @@ public class ManualTimerExampleTest extends JUnitSuite {

private final ManualTime manualTime = ManualTime.get(testKit.system());

static final class Tick {}
static final class Tick {
private Tick() {}

static final Tick INSTANCE = new Tick();
}

static final class Tock {}

Expand All @@ -40,7 +44,7 @@ public void testScheduleNonRepeatedTicks() {
Behavior<Tick> behavior =
Behaviors.withTimers(
timer -> {
timer.startSingleTimer("T", new Tick(), Duration.ofMillis(10));
timer.startSingleTimer(Tick.INSTANCE, Duration.ofMillis(10));
return Behaviors.receiveMessage(
tick -> {
probe.ref().tell(new Tock());
Expand Down
Expand Up @@ -47,7 +47,7 @@ class TestProbeSpec extends ScalaTestWithActorTestKit with WordSpecLike with Log
val probe = TestProbe()
val ref = spawn(Behaviors.receive[Stop.type]((_, _) =>
Behaviors.withTimers { timer =>
timer.startSingleTimer("key", Stop, 300.millis)
timer.startSingleTimer(Stop, 300.millis)

Behaviors.receive((_, _) => Behaviors.stopped)
}))
Expand Down
Expand Up @@ -25,7 +25,7 @@ class ManualTimerExampleSpec extends ScalaTestWithActorTestKit(ManualTime.config

val probe = TestProbe[Tock.type]()
val behavior = Behaviors.withTimers[Tick.type] { timer =>
timer.startSingleTimer("T", Tick, 10.millis)
timer.startSingleTimer(Tick, 10.millis)
Behaviors.receiveMessage { _ =>
probe.ref ! Tock
Behaviors.same
Expand All @@ -49,7 +49,7 @@ class ManualTimerExampleSpec extends ScalaTestWithActorTestKit(ManualTime.config

val probe = TestProbe[Tock.type]()
val behavior = Behaviors.withTimers[Tick.type] { timer =>
timer.startTimerWithFixedDelay("T", Tick, 10.millis)
timer.startTimerWithFixedDelay(Tick, 10.millis)
Behaviors.receiveMessage { _ =>
probe.ref ! Tock
Behaviors.same
Expand Down
Expand Up @@ -133,6 +133,15 @@ public Behavior<MyMsg> aroundSignal(
});
}

{
Behavior<MyMsg> b =
Behaviors.withTimers(
timers -> {
timers.startTimerWithFixedDelay(new MyMsgB("tick"), Duration.ofSeconds(1));
return Behaviors.ignore();
});
}

static class MyBehavior extends ExtensibleBehavior<MyMsg> {

@Override
Expand Down
Expand Up @@ -211,7 +211,7 @@ private static Behavior<Command> onIncrementRepeatedly(
name,
command.interval,
n);
timers.startTimerWithFixedDelay("repeat", Increment.INSTANCE, command.interval);
timers.startTimerWithFixedDelay(Increment.INSTANCE, command.interval);
return Behaviors.same();
}

Expand Down Expand Up @@ -306,7 +306,7 @@ private static Behavior<Command> onIncrementRepeatedly(
setup.name,
command.interval,
n);
setup.timers.startTimerWithFixedDelay("repeat", Increment.INSTANCE, command.interval);
setup.timers.startTimerWithFixedDelay(Increment.INSTANCE, command.interval);
return Behaviors.same();
}

Expand Down Expand Up @@ -394,7 +394,7 @@ private Behavior<Command> onIncrementRepeatedly(int n, IncrementRepeatedly comma
name,
command.interval,
n);
timers.startTimerWithFixedDelay("repeat", Increment.INSTANCE, command.interval);
timers.startTimerWithFixedDelay(Increment.INSTANCE, command.interval);
return Behaviors.same();
}

Expand Down Expand Up @@ -552,7 +552,7 @@ public static Behavior<Command> create(String name, Duration tickInterval) {
context ->
Behaviors.withTimers(
timers -> {
timers.startTimerWithFixedDelay("tick", Tick.INSTANCE, tickInterval);
timers.startTimerWithFixedDelay(Tick.INSTANCE, tickInterval);
return new Counter(name, context);
}));
}
Expand Down Expand Up @@ -688,7 +688,7 @@ public static Behavior<Command> create(String name, Duration tickInterval) {
(ActorContext<Message> context) ->
Behaviors.withTimers(
timers -> {
timers.startTimerWithFixedDelay("tick", Tick.INSTANCE, tickInterval);
timers.startTimerWithFixedDelay(Tick.INSTANCE, tickInterval);
return new Counter(name, context);
}))
.narrow(); // note narrow here
Expand Down
Expand Up @@ -88,7 +88,7 @@ class TimerSpec extends ScalaTestWithActorTestKit with WordSpecLike with LogCapt
"schedule non-repeated ticks" taggedAs TimingTest in {
val probe = TestProbe[Event]("evt")
val behv = Behaviors.withTimers[Command] { timer =>
timer.startSingleTimer("T", Tick(1), 10.millis)
timer.startSingleTimer(Tick(1), 10.millis)
target(probe.ref, timer, 1)
}

Expand All @@ -103,7 +103,7 @@ class TimerSpec extends ScalaTestWithActorTestKit with WordSpecLike with LogCapt
"schedule repeated ticks" taggedAs TimingTest in {
val probe = TestProbe[Event]("evt")
val behv = Behaviors.withTimers[Command] { timer =>
timer.startTimerWithFixedDelay("T", Tick(1), interval)
timer.startTimerWithFixedDelay(Tick(1), interval)
target(probe.ref, timer, 1)
}

Expand All @@ -121,7 +121,7 @@ class TimerSpec extends ScalaTestWithActorTestKit with WordSpecLike with LogCapt
"replace timer" taggedAs TimingTest in {
val probe = TestProbe[Event]("evt")
val behv = Behaviors.withTimers[Command] { timer =>
timer.startTimerWithFixedDelay("T", Tick(1), interval)
timer.startTimerWithFixedDelay(Tick(1), interval)
target(probe.ref, timer, 1)
}

Expand All @@ -141,7 +141,7 @@ class TimerSpec extends ScalaTestWithActorTestKit with WordSpecLike with LogCapt
"cancel timer" taggedAs TimingTest in {
val probe = TestProbe[Event]("evt")
val behv = Behaviors.withTimers[Command] { timer =>
timer.startTimerWithFixedDelay("T", Tick(1), interval)
timer.startTimerWithFixedDelay(Tick(1), interval)
target(probe.ref, timer, 1)
}

Expand Down Expand Up @@ -193,7 +193,7 @@ class TimerSpec extends ScalaTestWithActorTestKit with WordSpecLike with LogCapt
val probe = TestProbe[Event]("evt")
val behv = Behaviors
.supervise(Behaviors.withTimers[Command] { timer =>
timer.startTimerWithFixedDelay("T", Tick(1), interval)
timer.startTimerWithFixedDelay(Tick(1), interval)
target(probe.ref, timer, 1)
})
.onFailure[Exception](SupervisorStrategy.restart)
Expand Down Expand Up @@ -222,7 +222,7 @@ class TimerSpec extends ScalaTestWithActorTestKit with WordSpecLike with LogCapt
"cancel timers when stopped from exception" taggedAs TimingTest in {
val probe = TestProbe[Event]()
val behv = Behaviors.withTimers[Command] { timer =>
timer.startTimerWithFixedDelay("T", Tick(1), interval)
timer.startTimerWithFixedDelay(Tick(1), interval)
target(probe.ref, timer, 1)
}
val ref = spawn(behv)
Expand All @@ -235,7 +235,7 @@ class TimerSpec extends ScalaTestWithActorTestKit with WordSpecLike with LogCapt
"cancel timers when stopped voluntarily" taggedAs TimingTest in {
val probe = TestProbe[Event]()
val behv = Behaviors.withTimers[Command] { timer =>
timer.startTimerWithFixedDelay("T", Tick(1), interval)
timer.startTimerWithFixedDelay(Tick(1), interval)
target(probe.ref, timer, 1)
}
val ref = spawn(behv)
Expand All @@ -246,9 +246,9 @@ class TimerSpec extends ScalaTestWithActorTestKit with WordSpecLike with LogCapt
"allow for nested timers" in {
val probe = TestProbe[String]()
val ref = spawn(Behaviors.withTimers[String] { outerTimer =>
outerTimer.startTimerWithFixedDelay("outer-key", "outer-message", 50.millis)
outerTimer.startTimerWithFixedDelay("outer-message", 50.millis)
Behaviors.withTimers { innerTimer =>
innerTimer.startTimerWithFixedDelay("inner-key", "inner-message", 50.millis)
innerTimer.startTimerWithFixedDelay("inner-message", 50.millis)
Behaviors.receiveMessage { message =>
if (message == "stop") Behaviors.stopped
else {
Expand All @@ -273,7 +273,7 @@ class TimerSpec extends ScalaTestWithActorTestKit with WordSpecLike with LogCapt
"keep timers when behavior changes" in {
val probe = TestProbe[String]()
def newBehavior(n: Int): Behavior[String] = Behaviors.withTimers[String] { timers =>
timers.startTimerWithFixedDelay(s"key${n}", s"message${n}", 50.milli)
timers.startTimerWithFixedDelay(s"message${n}", 50.milli)
Behaviors.receiveMessage { message =>
if (message == "stop") Behaviors.stopped
else {
Expand All @@ -299,7 +299,7 @@ class TimerSpec extends ScalaTestWithActorTestKit with WordSpecLike with LogCapt
val probe = TestProbe[DeadLetter]()
val ref = spawn(Behaviors.withTimers[String] { timers =>
Behaviors.setup { _ =>
timers.startTimerWithFixedDelay("test", "test", 250.millis)
timers.startTimerWithFixedDelay("test", 250.millis)
Behaviors.receive { (context, _) =>
Behaviors.stopped(() => context.log.info(s"stopping"))
}
Expand All @@ -323,11 +323,11 @@ class TimerSpec extends ScalaTestWithActorTestKit with WordSpecLike with LogCapt
case Tick(-1) =>
probe.ref ! Tock(-1)
Behaviors.withTimers[Command] { timer =>
timer.startSingleTimer("T0", Tick(0), 5.millis)
timer.startSingleTimer(Tick(0), 5.millis)
Behaviors.receiveMessage[Command] {
case Tick(0) =>
probe.ref ! Tock(0)
timer.startSingleTimer("T1", Tick(1), 5.millis)
timer.startSingleTimer(Tick(1), 5.millis)
// let Tick(0) arrive in mailbox, test will not fail if it arrives later
Thread.sleep(100)
throw TestException("boom")
Expand Down Expand Up @@ -365,7 +365,7 @@ class TimerSpec extends ScalaTestWithActorTestKit with WordSpecLike with LogCapt
case Tick(-1) =>
probe.ref ! Tock(-1)
Behaviors.withTimers[Command] { timer =>
timer.startSingleTimer("T0", Tick(0), 5.millis)
timer.startSingleTimer(Tick(0), 5.millis)
// let Tick(0) arrive in mailbox, test will not fail if it arrives later
Thread.sleep(100)
throw TestException("boom")
Expand Down
Expand Up @@ -148,7 +148,7 @@ class TransformMessagesSpec extends ScalaTestWithActorTestKit with WordSpecLike
val probe = TestProbe[String]()
val behv = Behaviors
.withTimers[String] { timers =>
timers.startSingleTimer("timer", "a", 10.millis)
timers.startSingleTimer("a", 10.millis)
Behaviors.receiveMessage { msg =>
probe.ref ! msg
Behaviors.same
Expand All @@ -169,7 +169,7 @@ class TransformMessagesSpec extends ScalaTestWithActorTestKit with WordSpecLike
"be possible to combine with outer timers" in {
val probe = TestProbe[String]()
val behv = Behaviors.withTimers[String] { timers =>
timers.startSingleTimer("timer", "a", 10.millis)
timers.startSingleTimer("a", 10.millis)
Behaviors
.receiveMessage[String] { msg =>
probe.ref ! msg
Expand Down
Expand Up @@ -57,7 +57,7 @@ object FSMDocSpec {
private def active(data: Todo): Behavior[Event] =
Behaviors.withTimers[Event] { timers =>
// instead of FSM state timeout
timers.startSingleTimer(Timeout, Timeout, 1.second)
timers.startSingleTimer(Timeout, 1.second)
Behaviors.receiveMessagePartial {
case Flush | Timeout =>
data.target ! Batch(data.queue)
Expand Down
Expand Up @@ -123,7 +123,7 @@ object StyleGuideDocExamples {
name,
interval.toString,
n.toString)
timers.startTimerWithFixedDelay("repeat", Increment, interval)
timers.startTimerWithFixedDelay(Increment, interval)
Behaviors.same
case Increment =>
val newValue = n + 1
Expand Down Expand Up @@ -166,7 +166,7 @@ object StyleGuideDocExamples {
setup.name,
interval,
n)
setup.timers.startTimerWithFixedDelay("repeat", Increment, interval)
setup.timers.startTimerWithFixedDelay(Increment, interval)
Behaviors.same
case Increment =>
val newValue = n + 1
Expand Down Expand Up @@ -213,7 +213,7 @@ object StyleGuideDocExamples {
name,
interval,
n)
timers.startTimerWithFixedDelay("repeat", Increment, interval)
timers.startTimerWithFixedDelay(Increment, interval)
Behaviors.same
case Increment =>
val newValue = n + 1
Expand Down Expand Up @@ -249,7 +249,7 @@ object StyleGuideDocExamples {
name,
interval,
n)
timers.startTimerWithFixedDelay("repeat", Increment, interval)
timers.startTimerWithFixedDelay(Increment, interval)
Behaviors.same
case Increment =>
val newValue = n + 1
Expand Down Expand Up @@ -341,7 +341,7 @@ object StyleGuideDocExamples {
def apply(name: String, tickInterval: FiniteDuration): Behavior[Command] =
Behaviors.setup { context =>
Behaviors.withTimers { timers =>
timers.startTimerWithFixedDelay("tick", Tick, tickInterval)
timers.startTimerWithFixedDelay(Tick, tickInterval)
new Counter(name, context).counter(0)
}
}
Expand Down Expand Up @@ -390,7 +390,7 @@ object StyleGuideDocExamples {
Behaviors
.setup[Counter.Message] { context =>
Behaviors.withTimers { timers =>
timers.startTimerWithFixedDelay("tick", Tick, tickInterval)
timers.startTimerWithFixedDelay(Tick, tickInterval)
new Counter(name, context).counter(0)
}
}
Expand Down
Expand Up @@ -46,9 +46,9 @@ object TailChopping {

def sendNextRequest(requestCount: Int): Behavior[Command] = {
if (sendRequest(requestCount, replyAdapter)) {
timers.startSingleTimer(RequestTimeout, RequestTimeout, nextRequestAfter)
timers.startSingleTimer(RequestTimeout, nextRequestAfter)
} else {
timers.startSingleTimer(FinalTimeout, FinalTimeout, finalTimeout)
timers.startSingleTimer(FinalTimeout, finalTimeout)
}
waiting(requestCount)
}
Expand Down

0 comments on commit 3f3d877

Please sign in to comment.