Quieten the stm test output
This commit is contained in:
parent
d5c0237b2f
commit
562ebd9490
10 changed files with 99 additions and 59 deletions
|
|
@ -10,7 +10,7 @@ public class UntypedCoordinatedCounter extends UntypedActor {
|
|||
private Ref<Integer> count = new Ref<Integer>(0);
|
||||
|
||||
private void increment() {
|
||||
System.out.println("incrementing");
|
||||
//System.out.println("incrementing");
|
||||
count.set(count.get() + 1);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,7 @@
|
|||
package akka.transactor.test;
|
||||
|
||||
public class ExpectedFailureException extends RuntimeException {
|
||||
public ExpectedFailureException() {
|
||||
super("Expected failure");
|
||||
}
|
||||
}
|
||||
|
|
@ -26,7 +26,7 @@ public class UntypedCoordinatedCounter extends UntypedActor {
|
|||
}
|
||||
|
||||
private void increment() {
|
||||
System.out.println(name + ": incrementing");
|
||||
//System.out.println(name + ": incrementing");
|
||||
count.set(count.get() + 1);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -10,13 +10,21 @@ import akka.actor.ActorRef;
|
|||
import akka.actor.UntypedActor;
|
||||
import akka.actor.UntypedActorFactory;
|
||||
import akka.dispatch.Future;
|
||||
import akka.event.EventHandler;
|
||||
import akka.testkit.EventFilter;
|
||||
import akka.testkit.ErrorFilter;
|
||||
import akka.testkit.TestEvent;
|
||||
import akka.transactor.CoordinatedTransactionException;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import scala.Option;
|
||||
import scala.collection.JavaConverters;
|
||||
import scala.collection.Seq;
|
||||
|
||||
public class UntypedCoordinatedIncrementTest {
|
||||
List<ActorRef> counters;
|
||||
|
|
@ -63,6 +71,10 @@ public class UntypedCoordinatedIncrementTest {
|
|||
}
|
||||
|
||||
@Test public void incrementNoCountersWithFailingTransaction() {
|
||||
EventFilter expectedFailureFilter = (EventFilter) new ErrorFilter(ExpectedFailureException.class);
|
||||
EventFilter coordinatedFilter = (EventFilter) new ErrorFilter(CoordinatedTransactionException.class);
|
||||
Seq<EventFilter> ignoreExceptions = seq(expectedFailureFilter, coordinatedFilter);
|
||||
EventHandler.notify(new TestEvent.Mute(ignoreExceptions));
|
||||
CountDownLatch incrementLatch = new CountDownLatch(numCounters);
|
||||
List<ActorRef> actors = new ArrayList<ActorRef>(counters);
|
||||
actors.add(failer);
|
||||
|
|
@ -83,6 +95,11 @@ public class UntypedCoordinatedIncrementTest {
|
|||
}
|
||||
}
|
||||
}
|
||||
EventHandler.notify(new TestEvent.UnMute(ignoreExceptions));
|
||||
}
|
||||
|
||||
public <A> Seq<A> seq(A... args) {
|
||||
return JavaConverters.collectionAsScalaIterableConverter(Arrays.asList(args)).asScala().toSeq();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ public class UntypedCounter extends UntypedTransactor {
|
|||
}
|
||||
|
||||
private void increment() {
|
||||
System.out.println(name + ": incrementing");
|
||||
//System.out.println(name + ": incrementing");
|
||||
count.set(count.get() + 1);
|
||||
}
|
||||
|
||||
|
|
@ -48,7 +48,7 @@ public class UntypedCounter extends UntypedTransactor {
|
|||
}
|
||||
|
||||
@Override public void before(Object message) {
|
||||
System.out.println(name + ": before transaction");
|
||||
//System.out.println(name + ": before transaction");
|
||||
}
|
||||
|
||||
public void atomically(Object message) {
|
||||
|
|
@ -65,7 +65,7 @@ public class UntypedCounter extends UntypedTransactor {
|
|||
}
|
||||
|
||||
@Override public void after(Object message) {
|
||||
System.out.println(name + ": after transaction");
|
||||
//System.out.println(name + ": after transaction");
|
||||
}
|
||||
|
||||
@Override public boolean normally(Object message) {
|
||||
|
|
|
|||
|
|
@ -4,6 +4,6 @@ import akka.transactor.UntypedTransactor;
|
|||
|
||||
public class UntypedFailer extends UntypedTransactor {
|
||||
public void atomically(Object message) throws Exception {
|
||||
throw new RuntimeException("Expected failure");
|
||||
throw new ExpectedFailureException();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,13 +9,21 @@ import akka.actor.Actors;
|
|||
import akka.actor.UntypedActor;
|
||||
import akka.actor.UntypedActorFactory;
|
||||
import akka.dispatch.Future;
|
||||
import akka.event.EventHandler;
|
||||
import akka.testkit.EventFilter;
|
||||
import akka.testkit.ErrorFilter;
|
||||
import akka.testkit.TestEvent;
|
||||
import akka.transactor.CoordinatedTransactionException;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import scala.Option;
|
||||
import scala.collection.JavaConverters;
|
||||
import scala.collection.Seq;
|
||||
|
||||
public class UntypedTransactorTest {
|
||||
List<ActorRef> counters;
|
||||
|
|
@ -62,6 +70,10 @@ public class UntypedTransactorTest {
|
|||
}
|
||||
|
||||
@Test public void incrementNoCountersWithFailingTransaction() {
|
||||
EventFilter expectedFailureFilter = (EventFilter) new ErrorFilter(ExpectedFailureException.class);
|
||||
EventFilter coordinatedFilter = (EventFilter) new ErrorFilter(CoordinatedTransactionException.class);
|
||||
Seq<EventFilter> ignoreExceptions = seq(expectedFailureFilter, coordinatedFilter);
|
||||
EventHandler.notify(new TestEvent.Mute(ignoreExceptions));
|
||||
CountDownLatch incrementLatch = new CountDownLatch(numCounters);
|
||||
List<ActorRef> actors = new ArrayList<ActorRef>(counters);
|
||||
actors.add(failer);
|
||||
|
|
@ -82,6 +94,11 @@ public class UntypedTransactorTest {
|
|||
}
|
||||
}
|
||||
}
|
||||
EventHandler.notify(new TestEvent.UnMute(ignoreExceptions));
|
||||
}
|
||||
|
||||
public <A> Seq<A> seq(A... args) {
|
||||
return JavaConverters.collectionAsScalaIterableConverter(Arrays.asList(args)).asScala().toSeq();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,9 +8,8 @@ import akka.transactor.Coordinated
|
|||
import akka.actor.{ Actor, ActorRef }
|
||||
import akka.stm.{ Ref, TransactionFactory }
|
||||
import akka.util.duration._
|
||||
import akka.event.EventHandler
|
||||
import akka.testkit.EventFilter
|
||||
import akka.testkit.TestEvent._
|
||||
import akka.transactor.CoordinatedTransactionException
|
||||
import akka.testkit._
|
||||
|
||||
object CoordinatedIncrement {
|
||||
case class Increment(friends: Seq[ActorRef])
|
||||
|
|
@ -39,13 +38,15 @@ object CoordinatedIncrement {
|
|||
}
|
||||
}
|
||||
|
||||
class ExpectedFailureException extends RuntimeException("Expected failure")
|
||||
|
||||
class Failer extends Actor {
|
||||
val txFactory = TransactionFactory(timeout = 3 seconds)
|
||||
|
||||
def receive = {
|
||||
case coordinated @ Coordinated(Increment(friends)) ⇒ {
|
||||
coordinated.atomic(txFactory) {
|
||||
throw new RuntimeException("Expected failure")
|
||||
throw new ExpectedFailureException
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -55,15 +56,6 @@ object CoordinatedIncrement {
|
|||
class CoordinatedIncrementSpec extends WordSpec with MustMatchers with BeforeAndAfterAll {
|
||||
import CoordinatedIncrement._
|
||||
|
||||
override def beforeAll() {
|
||||
EventHandler notify Mute(EventFilter[RuntimeException]("Expected failure"))
|
||||
EventHandler notify Mute(EventFilter[org.multiverse.api.exceptions.DeadTransactionException]())
|
||||
}
|
||||
|
||||
override def afterAll() {
|
||||
EventHandler notify UnMuteAll
|
||||
}
|
||||
|
||||
val numCounters = 5
|
||||
val timeout = 5 seconds
|
||||
|
||||
|
|
@ -88,15 +80,19 @@ class CoordinatedIncrementSpec extends WordSpec with MustMatchers with BeforeAnd
|
|||
}
|
||||
|
||||
"increment no counters with a failing transaction" in {
|
||||
val (counters, failer) = createActors
|
||||
val coordinated = Coordinated()
|
||||
counters(0) ! Coordinated(Increment(counters.tail :+ failer))
|
||||
coordinated.await
|
||||
for (counter ← counters) {
|
||||
(counter ? GetCount).as[Int].get must be === 0
|
||||
filterException[ExpectedFailureException] {
|
||||
filterException[CoordinatedTransactionException] {
|
||||
val (counters, failer) = createActors
|
||||
val coordinated = Coordinated()
|
||||
counters(0) ! Coordinated(Increment(counters.tail :+ failer))
|
||||
coordinated.await
|
||||
for (counter ← counters) {
|
||||
(counter ? GetCount).as[Int].get must be === 0
|
||||
}
|
||||
counters foreach (_.stop())
|
||||
failer.stop()
|
||||
}
|
||||
}
|
||||
counters foreach (_.stop())
|
||||
failer.stop()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,9 +8,8 @@ import akka.transactor.Coordinated
|
|||
import akka.actor.{ Actor, ActorRef }
|
||||
import akka.stm._
|
||||
import akka.util.duration._
|
||||
import akka.event.EventHandler
|
||||
import akka.testkit.EventFilter
|
||||
import akka.testkit.TestEvent._
|
||||
import akka.transactor.CoordinatedTransactionException
|
||||
import akka.testkit._
|
||||
|
||||
import scala.util.Random.{ nextInt ⇒ random }
|
||||
|
||||
|
|
@ -59,6 +58,8 @@ object FickleFriends {
|
|||
}
|
||||
}
|
||||
|
||||
class ExpectedFailureException(message: String) extends RuntimeException(message)
|
||||
|
||||
/**
|
||||
* FickleCounter randomly fails at different points with 50% chance of failing overall.
|
||||
*/
|
||||
|
|
@ -72,7 +73,7 @@ object FickleFriends {
|
|||
}
|
||||
|
||||
def failIf(x: Int, y: Int) = {
|
||||
if (x == y) throw new RuntimeException("Random fail at position " + x)
|
||||
if (x == y) throw new ExpectedFailureException("Random fail at position " + x)
|
||||
}
|
||||
|
||||
def receive = {
|
||||
|
|
@ -98,16 +99,6 @@ object FickleFriends {
|
|||
class FickleFriendsSpec extends WordSpec with MustMatchers with BeforeAndAfterAll {
|
||||
import FickleFriends._
|
||||
|
||||
val ignoreEvents = List(EventFilter(classOf[RuntimeException], message = "Random fail"))
|
||||
|
||||
override def beforeAll() {
|
||||
ignoreEvents foreach (f ⇒ EventHandler.notify(Mute(f)))
|
||||
}
|
||||
|
||||
override def afterAll() {
|
||||
ignoreEvents foreach (f ⇒ EventHandler.notify(UnMute(f)))
|
||||
}
|
||||
|
||||
val numCounters = 2
|
||||
|
||||
def createActors = {
|
||||
|
|
@ -119,16 +110,20 @@ class FickleFriendsSpec extends WordSpec with MustMatchers with BeforeAndAfterAl
|
|||
|
||||
"Coordinated fickle friends" should {
|
||||
"eventually succeed to increment all counters by one" in {
|
||||
val (counters, coordinator) = createActors
|
||||
val latch = new CountDownLatch(1)
|
||||
coordinator ! FriendlyIncrement(counters, latch)
|
||||
latch.await // this could take a while
|
||||
(coordinator ? GetCount).as[Int].get must be === 1
|
||||
for (counter ← counters) {
|
||||
(counter ? GetCount).as[Int].get must be === 1
|
||||
filterException[ExpectedFailureException] {
|
||||
filterException[CoordinatedTransactionException] {
|
||||
val (counters, coordinator) = createActors
|
||||
val latch = new CountDownLatch(1)
|
||||
coordinator ! FriendlyIncrement(counters, latch)
|
||||
latch.await // this could take a while
|
||||
(coordinator ? GetCount).as[Int].get must be === 1
|
||||
for (counter ← counters) {
|
||||
(counter ? GetCount).as[Int].get must be === 1
|
||||
}
|
||||
counters foreach (_.stop())
|
||||
coordinator.stop()
|
||||
}
|
||||
}
|
||||
counters foreach (_.stop())
|
||||
coordinator.stop()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@ import akka.transactor.Transactor
|
|||
import akka.actor.{ Actor, ActorRef }
|
||||
import akka.stm._
|
||||
import akka.util.duration._
|
||||
import akka.transactor.CoordinatedTransactionException
|
||||
import akka.testkit._
|
||||
|
||||
import java.util.concurrent.CountDownLatch
|
||||
|
||||
|
|
@ -51,9 +53,11 @@ object TransactorIncrement {
|
|||
}
|
||||
}
|
||||
|
||||
class ExpectedFailureException extends RuntimeException("Expected failure")
|
||||
|
||||
class Failer extends Transactor {
|
||||
def atomically = {
|
||||
case _ ⇒ throw new RuntimeException("Expected failure")
|
||||
case _ ⇒ throw new ExpectedFailureException
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -99,15 +103,19 @@ class TransactorSpec extends WordSpec with MustMatchers {
|
|||
}
|
||||
|
||||
"increment no counters with a failing transaction" in {
|
||||
val (counters, failer) = createTransactors
|
||||
val failLatch = new CountDownLatch(numCounters + 1)
|
||||
counters(0) ! Increment(counters.tail :+ failer, failLatch)
|
||||
failLatch.await(timeout.length, timeout.unit)
|
||||
for (counter ← counters) {
|
||||
(counter ? GetCount).as[Int].get must be === 0
|
||||
filterException[ExpectedFailureException] {
|
||||
filterException[CoordinatedTransactionException] {
|
||||
val (counters, failer) = createTransactors
|
||||
val failLatch = new CountDownLatch(numCounters + 1)
|
||||
counters(0) ! Increment(counters.tail :+ failer, failLatch)
|
||||
failLatch.await(timeout.length, timeout.unit)
|
||||
for (counter ← counters) {
|
||||
(counter ? GetCount).as[Int].get must be === 0
|
||||
}
|
||||
counters foreach (_.stop())
|
||||
failer.stop()
|
||||
}
|
||||
}
|
||||
counters foreach (_.stop())
|
||||
failer.stop()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue