Skip to content

Commit

Permalink
Fixing stateless actors timers.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSperry committed Dec 9, 2015
1 parent a670cbc commit c83750b
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
2 changes: 1 addition & 1 deletion actors/stage/src/main/java/com/ea/orbit/actors/Stage.java
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,7 @@ public Registration registerTimer(final AbstractActor<?> actor,
final Object key = actor.getClass().isAnnotationPresent(StatelessWorker.class)
? actor : RemoteReference.from(actor);

final ActorEntry localActor = (ActorEntry) objects.findLocalActor((Actor) actor);
final ActorBaseEntry localActor = (ActorBaseEntry) objects.findLocalActor((Actor) actor);

if (localActor == null || localActor.isDeactivated())
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
Copyright (C) 2015 Electronic Arts Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of Electronic Arts, Inc. ("EA") nor the names of
its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY ELECTRONIC ARTS AND ITS CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL ELECTRONIC ARTS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.ea.orbit.actors.test.stateless;

import com.ea.orbit.actors.Actor;
import com.ea.orbit.actors.Stage;
import com.ea.orbit.actors.annotation.StatelessWorker;
import com.ea.orbit.actors.runtime.AbstractActor;
import com.ea.orbit.actors.test.ActorBaseTest;
import com.ea.orbit.actors.test.FakeSync;
import com.ea.orbit.concurrent.Task;

import org.junit.Test;

import javax.inject.Inject;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertEquals;

public class StatelessWithTimersTest extends ActorBaseTest
{
@StatelessWorker
public interface StatelessTimed extends Actor
{
Task<String> scheduleSomething(String msg);
}

public static class StatelessTimedActor extends AbstractActor implements StatelessTimed
{
@Inject
private FakeSync sync;

@Override
public Task<String> scheduleSomething(String msg)
{
registerTimer(() -> {
sync.task("name").complete(msg);
return Task.done();
}, 100, 100, TimeUnit.MILLISECONDS);
return Task.fromValue(msg);
}

@Override
public Task<?> deactivateAsync()
{
sync.semaphore("deactivation").release();
return super.deactivateAsync();
}
}

@Test
public void scheduleSimpleTimer() throws ExecutionException, InterruptedException
{
createStage();
StatelessTimed actor1 = Actor.getReference(StatelessTimed.class, "1000");
assertEquals("hello", actor1.scheduleSomething("hello").join());
// this will be released when the timer is completed
fakeSync.task("name").join();
}

@Test
public void scheduleSimpleTimerAndStopStage() throws ExecutionException, InterruptedException
{
final Stage stage1 = createStage();
StatelessTimed actor1 = Actor.getReference(StatelessTimed.class, "1000");
assertEquals("hello", actor1.scheduleSomething("hello").join());
// this will be released when the timer is completed
fakeSync.task("name").join();
stage1.stop().join();
}

}

0 comments on commit c83750b

Please sign in to comment.