diff --git a/akka-actor-tests/src/test/java/akka/event/LoggingAdapterTest.java b/akka-actor-tests/src/test/java/akka/event/LoggingAdapterTest.java index 0af4fca970..4b93e675ed 100644 --- a/akka-actor-tests/src/test/java/akka/event/LoggingAdapterTest.java +++ b/akka-actor-tests/src/test/java/akka/event/LoggingAdapterTest.java @@ -3,6 +3,7 @@ package akka.event; import akka.actor.ActorRef; import akka.actor.ActorSystem; import akka.actor.Props; +import akka.testkit.AkkaJUnitActorSystemResource; import akka.testkit.JavaTestKit; import akka.event.Logging.Error; import akka.event.ActorWithMDC.Log; @@ -10,6 +11,8 @@ import static akka.event.Logging.*; import com.typesafe.config.Config; import com.typesafe.config.ConfigFactory; +import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.scalatest.junit.JUnitSuite; import scala.concurrent.duration.Duration; @@ -30,9 +33,19 @@ public class LoggingAdapterTest extends JUnitSuite { "akka.actor.serialize-messages = off" ); + @Rule + public AkkaJUnitActorSystemResource actorSystemResource = + new AkkaJUnitActorSystemResource("LoggingAdapterTest", config); + + private ActorSystem system = null; + + @Before + public void beforeEach() { + system = actorSystemResource.getSystem(); + } + @Test public void mustFormatMessage() { - final ActorSystem system = ActorSystem.create("test-system", config); final LoggingAdapter log = Logging.getLogger(system, this); new LogJavaTestKit(system) {{ system.eventStream().subscribe(getRef(), LogEvent.class); @@ -66,7 +79,6 @@ public class LoggingAdapterTest extends JUnitSuite { @Test public void mustCallMdcForEveryLog() throws Exception { - final ActorSystem system = ActorSystem.create("test-system", config); new LogJavaTestKit(system) {{ system.eventStream().subscribe(getRef(), LogEvent.class); ActorRef ref = system.actorOf(Props.create(ActorWithMDC.class)); @@ -86,7 +98,6 @@ public class LoggingAdapterTest extends JUnitSuite { @Test public void mustSupportMdcNull() throws Exception { - final ActorSystem system = ActorSystem.create("test-system", config); new LogJavaTestKit(system) {{ system.eventStream().subscribe(getRef(), LogEvent.class); ActorRef ref = system.actorOf(Props.create(ActorWithMDC.class)); @@ -131,6 +142,7 @@ public class LoggingAdapterTest extends JUnitSuite { void expectLog(final Object level, final String message, final Throwable cause, final String mdc) { new ExpectMsg(Duration.create(3, TimeUnit.SECONDS), "LogEvent") { + @Override protected Void match(Object event) { LogEvent log = (LogEvent) event; assertEquals(message, log.message()); diff --git a/akka-actor-tests/src/test/scala/akka/io/TcpConnectionSpec.scala b/akka-actor-tests/src/test/scala/akka/io/TcpConnectionSpec.scala index 8435ab9614..dd324fb596 100644 --- a/akka-actor-tests/src/test/scala/akka/io/TcpConnectionSpec.scala +++ b/akka-actor-tests/src/test/scala/akka/io/TcpConnectionSpec.scala @@ -360,11 +360,10 @@ class TcpConnectionSpec extends AkkaSpec(""" "respect pull mode" in new EstablishedConnectionTest(pullMode = true) { // override config to decrease default buffer size - val config = - ConfigFactory.load( - ConfigFactory.parseString("akka.io.tcp.direct-buffer-size = 1k") - .withFallback(AkkaSpec.testConf)) - override implicit def system: ActorSystem = ActorSystem("respectPullModeTest", config) + def config = + ConfigFactory.parseString("akka.io.tcp.direct-buffer-size = 1k") + .withFallback(AkkaSpec.testConf) + override implicit lazy val system: ActorSystem = ActorSystem("respectPullModeTest", config) try run { val maxBufferSize = 1 * 1024 @@ -402,7 +401,7 @@ class TcpConnectionSpec extends AkkaSpec(""" connectionHandler.expectMsgType[Received].data.decodeString("ASCII") should ===(vs) } - finally system.terminate() + finally shutdown(system) } "close the connection and reply with `Closed` upon reception of a `Close` command" in diff --git a/akka-camel/src/test/java/akka/camel/CustomRouteTest.java b/akka-camel/src/test/java/akka/camel/CustomRouteTest.java index eeb090b6cb..262c0ea5f5 100644 --- a/akka-camel/src/test/java/akka/camel/CustomRouteTest.java +++ b/akka-camel/src/test/java/akka/camel/CustomRouteTest.java @@ -23,16 +23,21 @@ import java.util.concurrent.TimeUnit; public class CustomRouteTest extends JUnitSuite { @Rule - public AkkaJUnitActorSystemResource actorSystemResource = - new AkkaJUnitActorSystemResource("CustomRouteTest"); + public AkkaJUnitActorSystemResource actorSystemResource = new AkkaJUnitActorSystemResource("CustomRouteTest"); - private final ActorSystem system = actorSystemResource.getSystem(); - private Camel camel = (Camel) CamelExtension.get(system); + private ActorSystem system = null; + private Camel camel = null; public static class MyActor extends UntypedActor { @Override public void onReceive(Object o) {} } + @Before + public void beforeEach() { + system = actorSystemResource.getSystem(); + camel = (Camel) CamelExtension.get(system); + } + @Test public void testCustomProducerRoute() throws Exception { MockEndpoint mockEndpoint = camel.context().getEndpoint("mock:mockProducer", MockEndpoint.class); @@ -118,6 +123,7 @@ public class CustomRouteTest extends JUnitSuite { private void assertMockEndpoint(MockEndpoint mockEndpoint) throws InterruptedException { mockEndpoint.expectedMessageCount(1); mockEndpoint.expectedMessagesMatches(new Predicate() { + @Override public boolean matches(Exchange exchange) { return exchange.getIn().getBody().equals("test"); } @@ -126,8 +132,8 @@ public class CustomRouteTest extends JUnitSuite { } public static class CustomRouteBuilder extends RouteBuilder { - private String uri; - private String fromUri; + private final String uri; + private final String fromUri; public CustomRouteBuilder(String from, String to) { fromUri = from; @@ -164,7 +170,7 @@ public class CustomRouteTest extends JUnitSuite { } public static class EndpointProducer extends UntypedProducerActor { - private String uri; + private final String uri; public EndpointProducer(String uri) { this.uri = uri; @@ -192,8 +198,8 @@ public class CustomRouteTest extends JUnitSuite { } public static class TestAckConsumer extends UntypedConsumerActor { - private String myuri; - private String to; + private final String myuri; + private final String to; public TestAckConsumer(String uri, String to){ myuri = uri; diff --git a/akka-camel/src/test/scala/akka/camel/TestSupport.scala b/akka-camel/src/test/scala/akka/camel/TestSupport.scala index e022b6434e..e61e807c82 100644 --- a/akka-camel/src/test/scala/akka/camel/TestSupport.scala +++ b/akka-camel/src/test/scala/akka/camel/TestSupport.scala @@ -48,7 +48,7 @@ private[camel] object TestSupport { } trait SharedCamelSystem extends BeforeAndAfterAll { this: Suite ⇒ - implicit lazy val system = ActorSystem("test", AkkaSpec.testConf) + implicit lazy val system = ActorSystem("SharedCamelSystem", AkkaSpec.testConf) implicit lazy val camel = CamelExtension(system) abstract override protected def afterAll() { @@ -63,7 +63,7 @@ private[camel] object TestSupport { override protected def beforeEach() { super.beforeEach() - system = ActorSystem("test", AkkaSpec.testConf) + system = ActorSystem("NonSharedCamelSystem", AkkaSpec.testConf) camel = CamelExtension(system) } diff --git a/akka-camel/src/test/scala/akka/camel/internal/ActivationTrackerTest.scala b/akka-camel/src/test/scala/akka/camel/internal/ActivationTrackerTest.scala index 7d0b2edc4d..858cd7c602 100644 --- a/akka-camel/src/test/scala/akka/camel/internal/ActivationTrackerTest.scala +++ b/akka-camel/src/test/scala/akka/camel/internal/ActivationTrackerTest.scala @@ -7,7 +7,7 @@ import akka.actor.{ Props, ActorSystem } import akka.testkit.{ TimingTest, TestProbe, TestKit } import akka.camel.internal.ActivationProtocol._ -class ActivationTrackerTest extends TestKit(ActorSystem("test")) with WordSpecLike with Matchers with BeforeAndAfterAll with BeforeAndAfterEach with GivenWhenThen { +class ActivationTrackerTest extends TestKit(ActorSystem("ActivationTrackerTest")) with WordSpecLike with Matchers with BeforeAndAfterAll with BeforeAndAfterEach with GivenWhenThen { override protected def afterAll() { shutdown() } diff --git a/akka-camel/src/test/scala/akka/camel/internal/component/ActorProducerTest.scala b/akka-camel/src/test/scala/akka/camel/internal/component/ActorProducerTest.scala index 86cbb742ad..889d3c697b 100644 --- a/akka-camel/src/test/scala/akka/camel/internal/component/ActorProducerTest.scala +++ b/akka-camel/src/test/scala/akka/camel/internal/component/ActorProducerTest.scala @@ -30,7 +30,7 @@ import akka.util.Timeout import akka.actor._ import akka.testkit._ -class ActorProducerTest extends TestKit(ActorSystem("test")) with WordSpecLike with Matchers with ActorProducerFixture { +class ActorProducerTest extends TestKit(ActorSystem("ActorProducerTest")) with WordSpecLike with Matchers with ActorProducerFixture { implicit val timeout = Timeout(10 seconds) "ActorProducer" when { diff --git a/akka-contrib/src/test/scala/akka/contrib/pattern/AggregatorSpec.scala b/akka-contrib/src/test/scala/akka/contrib/pattern/AggregatorSpec.scala index 0831e5e16a..31a826f587 100644 --- a/akka-contrib/src/test/scala/akka/contrib/pattern/AggregatorSpec.scala +++ b/akka-contrib/src/test/scala/akka/contrib/pattern/AggregatorSpec.scala @@ -7,13 +7,11 @@ import akka.testkit.{ ImplicitSender, TestKit } import org.scalatest.FunSuiteLike import org.scalatest.Matchers import scala.annotation.tailrec - -//#demo-code import scala.collection._ import scala.concurrent.duration._ import scala.math.BigDecimal.int2bigDecimal - import akka.actor._ +import org.scalatest.BeforeAndAfterAll /** * Sample and test code for the aggregator patter. * This is based on Jamie Allen's tutorial at @@ -187,7 +185,11 @@ class ChainingSample extends Actor with Aggregator { } //#chain-sample -class AggregatorSpec extends TestKit(ActorSystem("test")) with ImplicitSender with FunSuiteLike with Matchers { +class AggregatorSpec extends TestKit(ActorSystem("AggregatorSpec")) with ImplicitSender with FunSuiteLike with Matchers with BeforeAndAfterAll { + + override def afterAll(): Unit = { + shutdown() + } test("Test request 1 account type") { system.actorOf(Props[AccountBalanceRetriever]) ! GetCustomerAccountBalances(1, Set(Savings)) @@ -384,4 +386,4 @@ class WorkListSpec extends FunSuiteLike { processed } } -} \ No newline at end of file +} diff --git a/akka-docs/rst/java/code/docs/actor/FaultHandlingTest.java b/akka-docs/rst/java/code/docs/actor/FaultHandlingTest.java index 88c5a940ff..245d2d48a9 100644 --- a/akka-docs/rst/java/code/docs/actor/FaultHandlingTest.java +++ b/akka-docs/rst/java/code/docs/actor/FaultHandlingTest.java @@ -151,7 +151,7 @@ public class FaultHandlingTest extends AbstractJavaTest { @BeforeClass public static void start() { - system = ActorSystem.create("test"); + system = ActorSystem.create("FaultHandlingTest"); } @AfterClass diff --git a/akka-docs/rst/java/code/docs/actor/SchedulerDocTest.java b/akka-docs/rst/java/code/docs/actor/SchedulerDocTest.java index d1483d11c6..78bfaa8c62 100644 --- a/akka-docs/rst/java/code/docs/actor/SchedulerDocTest.java +++ b/akka-docs/rst/java/code/docs/actor/SchedulerDocTest.java @@ -22,10 +22,10 @@ import akka.testkit.AkkaJUnitActorSystemResource; import org.junit.*; public class SchedulerDocTest extends AbstractJavaTest { - - @Rule - public AkkaJUnitActorSystemResource actorSystemResource = - new AkkaJUnitActorSystemResource("SchedulerDocTest", AkkaSpec.testConf()); + + @ClassRule + public static AkkaJUnitActorSystemResource actorSystemResource = new AkkaJUnitActorSystemResource("SchedulerDocTest", + AkkaSpec.testConf()); private final ActorSystem system = actorSystemResource.getSystem(); private ActorRef testActor = system.actorOf(Props.create(MyUntypedActor.class)); diff --git a/akka-docs/rst/java/code/docs/camel/CustomRouteTestBase.java b/akka-docs/rst/java/code/docs/camel/CustomRouteTestBase.java index 2030ca3368..68a7a6e581 100644 --- a/akka-docs/rst/java/code/docs/camel/CustomRouteTestBase.java +++ b/akka-docs/rst/java/code/docs/camel/CustomRouteTestBase.java @@ -11,11 +11,14 @@ public class CustomRouteTestBase { public void customRoute() throws Exception{ //#CustomRoute ActorSystem system = ActorSystem.create("some-system"); - Camel camel = CamelExtension.get(system); - ActorRef responder = system.actorOf(Props.create(Responder.class), "TestResponder"); - camel.context().addRoutes(new CustomRouteBuilder(responder)); - //#CustomRoute - system.stop(responder); - JavaTestKit.shutdownActorSystem(system); + try { + Camel camel = CamelExtension.get(system); + ActorRef responder = system.actorOf(Props.create(Responder.class), "TestResponder"); + camel.context().addRoutes(new CustomRouteBuilder(responder)); + //#CustomRoute + system.stop(responder); + } finally { + JavaTestKit.shutdownActorSystem(system); + } } } diff --git a/akka-docs/rst/java/code/docs/pattern/SchedulerPatternTest.java b/akka-docs/rst/java/code/docs/pattern/SchedulerPatternTest.java index 0def5e514e..01897cf545 100644 --- a/akka-docs/rst/java/code/docs/pattern/SchedulerPatternTest.java +++ b/akka-docs/rst/java/code/docs/pattern/SchedulerPatternTest.java @@ -130,6 +130,11 @@ public class SchedulerPatternTest extends AbstractJavaTest { testSchedule(probe, props, duration("3000 millis"), duration("2500 millis")); }}; } + + @Test + public void doNothing() { + // actorSystemResource.after is not called when all tests are ignored + } public static class TestSchedule extends JavaTestKit { private ActorSystem system; diff --git a/akka-docs/rst/scala/code/docs/io/ScalaUdpMulticastSpec.scala b/akka-docs/rst/scala/code/docs/io/ScalaUdpMulticastSpec.scala index ab3cb5eb59..404c4941de 100644 --- a/akka-docs/rst/scala/code/docs/io/ScalaUdpMulticastSpec.scala +++ b/akka-docs/rst/scala/code/docs/io/ScalaUdpMulticastSpec.scala @@ -6,16 +6,15 @@ package docs.io import java.net.{ Inet6Address, InetSocketAddress, NetworkInterface, StandardProtocolFamily } import java.nio.channels.DatagramChannel - import scala.util.Random import akka.actor.{ ActorSystem, Props } import akka.io.Udp import akka.testkit.TestKit import org.scalatest.{ BeforeAndAfter, WordSpecLike } - import scala.collection.JavaConversions.enumerationAsScalaIterator +import org.scalatest.BeforeAndAfterAll -class ScalaUdpMulticastSpec extends TestKit(ActorSystem("ScalaUdpMulticastSpec")) with WordSpecLike with BeforeAndAfter { +class ScalaUdpMulticastSpec extends TestKit(ActorSystem("ScalaUdpMulticastSpec")) with WordSpecLike with BeforeAndAfterAll { "listener" should { "send message back to sink" in { @@ -65,7 +64,7 @@ class ScalaUdpMulticastSpec extends TestKit(ActorSystem("ScalaUdpMulticastSpec") } } - def afterAll(): Unit = { + override def afterAll(): Unit = { TestKit.shutdownActorSystem(system) } diff --git a/akka-docs/rst/scala/code/docs/testkit/ParentChildSpec.scala b/akka-docs/rst/scala/code/docs/testkit/ParentChildSpec.scala index e4f785e4c3..2219ca10ad 100644 --- a/akka-docs/rst/scala/code/docs/testkit/ParentChildSpec.scala +++ b/akka-docs/rst/scala/code/docs/testkit/ParentChildSpec.scala @@ -11,6 +11,8 @@ import akka.testkit.ImplicitSender import akka.testkit.TestProbe import akka.testkit.TestActorRef import akka.actor.ActorRefFactory +import akka.testkit.TestKit +import org.scalatest.BeforeAndAfterAll /** * Parent-Child examples @@ -74,8 +76,12 @@ class MockedChild extends Actor { } } -class ParentChildSpec extends WordSpec with Matchers with TestKitBase { - implicit lazy val system = ActorSystem() +class ParentChildSpec extends WordSpec with Matchers with TestKitBase with BeforeAndAfterAll { + implicit lazy val system = ActorSystem("ParentChildSpec") + + override def afterAll(): Unit = { + TestKit.shutdownActorSystem(system) + } "A DependentChild" should { "be tested without its parent" in { @@ -132,4 +138,4 @@ class ParentChildSpec extends WordSpec with Matchers with TestKitBase { } } //#test-fabricated-parent -} \ No newline at end of file +} diff --git a/akka-http-core/src/test/scala/akka/http/scaladsl/ClientSpec.scala b/akka-http-core/src/test/scala/akka/http/scaladsl/ClientSpec.scala index 166cca74cd..abff177431 100644 --- a/akka-http-core/src/test/scala/akka/http/scaladsl/ClientSpec.scala +++ b/akka-http-core/src/test/scala/akka/http/scaladsl/ClientSpec.scala @@ -10,11 +10,12 @@ import akka.http.scaladsl.model.HttpMethods._ import akka.stream.ActorMaterializer import com.typesafe.config.{ Config, ConfigFactory } import org.scalatest.{ Matchers, WordSpec } - import scala.concurrent.duration._ import scala.concurrent.{ Await } +import org.scalatest.BeforeAndAfterAll +import akka.testkit.TestKit -class ClientSpec extends WordSpec with Matchers { +class ClientSpec extends WordSpec with Matchers with BeforeAndAfterAll { val testConf: Config = ConfigFactory.parseString(""" akka.loggers = ["akka.testkit.TestEventListener"] akka.loglevel = ERROR @@ -25,6 +26,10 @@ class ClientSpec extends WordSpec with Matchers { implicit val system = ActorSystem(getClass.getSimpleName, testConf) implicit val materializer = ActorMaterializer() + override def afterAll(): Unit = { + TestKit.shutdownActorSystem(system) + } + "HTTP Client" should { "reuse connection pool" in { diff --git a/akka-http-core/src/test/scala/akka/http/scaladsl/TightRequestTimeoutSpec.scala b/akka-http-core/src/test/scala/akka/http/scaladsl/TightRequestTimeoutSpec.scala index 1902df0cc2..30fd489929 100644 --- a/akka-http-core/src/test/scala/akka/http/scaladsl/TightRequestTimeoutSpec.scala +++ b/akka-http-core/src/test/scala/akka/http/scaladsl/TightRequestTimeoutSpec.scala @@ -7,7 +7,6 @@ package akka.http.scaladsl import java.io.{ BufferedReader, BufferedWriter, InputStreamReader, OutputStreamWriter } import java.net.{ BindException, Socket } import java.util.concurrent.TimeoutException - import akka.actor.ActorSystem import akka.event.Logging import akka.event.Logging.LogEvent @@ -26,11 +25,11 @@ import akka.util.ByteString import com.typesafe.config.{ Config, ConfigFactory } import org.scalatest.concurrent.ScalaFutures import org.scalatest.{ BeforeAndAfterAll, Matchers, WordSpec } - import scala.annotation.tailrec import scala.concurrent.duration._ import scala.concurrent.{ Await, Future, Promise } import scala.util.{ Success, Try } +import akka.testkit.TestKit class TightRequestTimeoutSpec extends WordSpec with Matchers with BeforeAndAfterAll with ScalaFutures { val testConf: Config = ConfigFactory.parseString(""" @@ -46,6 +45,10 @@ class TightRequestTimeoutSpec extends WordSpec with Matchers with BeforeAndAfter implicit val materializer = ActorMaterializer() implicit val patience = PatienceConfig(3.seconds) + override def afterAll(): Unit = { + TestKit.shutdownActorSystem(system) + } + "Tight request timeout" should { "not cause double push error caused by the late response attemting to push" in { @@ -65,4 +68,4 @@ class TightRequestTimeoutSpec extends WordSpec with Matchers with BeforeAndAfter } } -} \ No newline at end of file +} diff --git a/akka-stream-tests/src/test/java/akka/stream/StreamTest.java b/akka-stream-tests/src/test/java/akka/stream/StreamTest.java index 9bcc8c8627..58c5e4f086 100644 --- a/akka-stream-tests/src/test/java/akka/stream/StreamTest.java +++ b/akka-stream-tests/src/test/java/akka/stream/StreamTest.java @@ -7,7 +7,7 @@ package akka.stream; import org.scalatest.junit.JUnitSuite; import akka.actor.ActorSystem; -import akka.stream.javadsl.AkkaJUnitActorSystemResource; +import akka.testkit.AkkaJUnitActorSystemResource; public abstract class StreamTest extends JUnitSuite { final protected ActorSystem system; diff --git a/akka-stream-tests/src/test/java/akka/stream/actor/ActorPublisherTest.java b/akka-stream-tests/src/test/java/akka/stream/actor/ActorPublisherTest.java index f9d24493de..6785a2a65a 100644 --- a/akka-stream-tests/src/test/java/akka/stream/actor/ActorPublisherTest.java +++ b/akka-stream-tests/src/test/java/akka/stream/actor/ActorPublisherTest.java @@ -3,7 +3,7 @@ package akka.stream.actor; import akka.actor.ActorRef; import akka.actor.Props; import akka.stream.StreamTest; -import akka.stream.javadsl.AkkaJUnitActorSystemResource; +import akka.testkit.AkkaJUnitActorSystemResource; import akka.stream.javadsl.Source; import akka.testkit.AkkaSpec; import akka.testkit.JavaTestKit; diff --git a/akka-stream-tests/src/test/java/akka/stream/actor/ActorSubscriberTest.java b/akka-stream-tests/src/test/java/akka/stream/actor/ActorSubscriberTest.java index 03b034c525..856f5f4deb 100644 --- a/akka-stream-tests/src/test/java/akka/stream/actor/ActorSubscriberTest.java +++ b/akka-stream-tests/src/test/java/akka/stream/actor/ActorSubscriberTest.java @@ -3,7 +3,7 @@ package akka.stream.actor; import akka.actor.ActorRef; import akka.actor.Props; import akka.stream.StreamTest; -import akka.stream.javadsl.AkkaJUnitActorSystemResource; +import akka.testkit.AkkaJUnitActorSystemResource; import akka.stream.javadsl.Sink; import akka.stream.javadsl.Source; import akka.testkit.AkkaSpec; diff --git a/akka-stream-tests/src/test/java/akka/stream/io/InputStreamSinkTest.java b/akka-stream-tests/src/test/java/akka/stream/io/InputStreamSinkTest.java index 96f5d0845c..f0060f9c3f 100644 --- a/akka-stream-tests/src/test/java/akka/stream/io/InputStreamSinkTest.java +++ b/akka-stream-tests/src/test/java/akka/stream/io/InputStreamSinkTest.java @@ -5,7 +5,7 @@ package akka.stream.io; import akka.japi.Pair; import akka.stream.StreamTest; -import akka.stream.javadsl.AkkaJUnitActorSystemResource; +import akka.testkit.AkkaJUnitActorSystemResource; import akka.stream.javadsl.Sink; import akka.stream.javadsl.Source; import akka.stream.javadsl.StreamConverters; diff --git a/akka-stream-tests/src/test/java/akka/stream/io/OutputStreamSinkTest.java b/akka-stream-tests/src/test/java/akka/stream/io/OutputStreamSinkTest.java index 674a6c5c93..45ff7ef1fe 100644 --- a/akka-stream-tests/src/test/java/akka/stream/io/OutputStreamSinkTest.java +++ b/akka-stream-tests/src/test/java/akka/stream/io/OutputStreamSinkTest.java @@ -5,7 +5,7 @@ package akka.stream.io; import akka.stream.IOResult; import akka.stream.StreamTest; -import akka.stream.javadsl.AkkaJUnitActorSystemResource; +import akka.testkit.AkkaJUnitActorSystemResource; import akka.stream.javadsl.Source; import akka.stream.javadsl.StreamConverters; import akka.stream.testkit.Utils; diff --git a/akka-stream-tests/src/test/java/akka/stream/io/OutputStreamSourceTest.java b/akka-stream-tests/src/test/java/akka/stream/io/OutputStreamSourceTest.java index d58d2ce714..85a84d5e37 100644 --- a/akka-stream-tests/src/test/java/akka/stream/io/OutputStreamSourceTest.java +++ b/akka-stream-tests/src/test/java/akka/stream/io/OutputStreamSourceTest.java @@ -14,7 +14,7 @@ import org.junit.Test; import akka.actor.ActorRef; import akka.japi.function.Procedure; import akka.stream.StreamTest; -import akka.stream.javadsl.AkkaJUnitActorSystemResource; +import akka.testkit.AkkaJUnitActorSystemResource; import akka.stream.javadsl.Sink; import akka.stream.javadsl.Source; import akka.stream.javadsl.StreamConverters; diff --git a/akka-stream-tests/src/test/java/akka/stream/io/SinkAsJavaSourceTest.java b/akka-stream-tests/src/test/java/akka/stream/io/SinkAsJavaSourceTest.java index ff8ceb2b53..da669ffbe0 100644 --- a/akka-stream-tests/src/test/java/akka/stream/io/SinkAsJavaSourceTest.java +++ b/akka-stream-tests/src/test/java/akka/stream/io/SinkAsJavaSourceTest.java @@ -5,7 +5,7 @@ package akka.stream.io; import akka.stream.StreamTest; -import akka.stream.javadsl.AkkaJUnitActorSystemResource; +import akka.testkit.AkkaJUnitActorSystemResource; import akka.stream.javadsl.Sink; import akka.stream.javadsl.Source; import akka.stream.javadsl.StreamConverters; diff --git a/akka-stream-tests/src/test/java/akka/stream/javadsl/AkkaJUnitActorSystemResource.java b/akka-stream-tests/src/test/java/akka/stream/javadsl/AkkaJUnitActorSystemResource.java deleted file mode 100644 index 057b341381..0000000000 --- a/akka-stream-tests/src/test/java/akka/stream/javadsl/AkkaJUnitActorSystemResource.java +++ /dev/null @@ -1,84 +0,0 @@ -/** - * Copyright (C) 2009-2016 Lightbend Inc. - */ -package akka.stream.javadsl; - -import org.junit.rules.ExternalResource; - -import akka.actor.ActorSystem; -import akka.testkit.AkkaSpec; -import akka.testkit.JavaTestKit; - -import com.typesafe.config.Config; - -// FIXME remove this copy, and use akka.testkit.AkkaJUnitActorSystemResource when -// akka-stream-experimental becomes a normal build project - -/** - * This is a resource for creating an actor system before test start and shut it - * down afterwards. - * - * To use it on a class level add this to your test class: - * - * @ClassRule public static AkkaJUnitActorSystemResource actorSystemResource = - * new AkkaJUnitActorSystemResource(name, config); - * - * private final ActorSystem system = - * actorSystemResource.getSystem(); - * - * - * To use it on a per test level add this to your test class: - * - * @Rule public AkkaJUnitActorSystemResource actorSystemResource = new - * AkkaJUnitActorSystemResource(name, config); - * - * private final ActorSystem system = actorSystemResource.getSystem(); - */ - -public class AkkaJUnitActorSystemResource extends ExternalResource { - private ActorSystem system = null; - private final String name; - private final Config config; - - private ActorSystem createSystem(String name, Config config) { - try { - if (config == null) - return ActorSystem.create(name); - else - return ActorSystem.create(name, config); - } catch (Exception e) { - e.printStackTrace(); - return null; - } - } - - public AkkaJUnitActorSystemResource(String name, Config config) { - this.name = name; - this.config = config; - system = createSystem(name, config); - } - - public AkkaJUnitActorSystemResource(String name) { - this(name, AkkaSpec.testConf()); - } - - @Override - protected void before() throws Throwable { - // Sometimes the ExternalResource seems to be reused, and - // we don't run the constructor again, so if that's the case - // then create the system here - if (system == null) { - system = createSystem(name, config); - } - } - - @Override - protected void after() { - JavaTestKit.shutdownActorSystem(system); - system = null; - } - - public ActorSystem getSystem() { - return system; - } -} diff --git a/akka-stream-tests/src/test/java/akka/stream/javadsl/AttributesTest.java b/akka-stream-tests/src/test/java/akka/stream/javadsl/AttributesTest.java index fba31ef0e6..511407dbfd 100644 --- a/akka-stream-tests/src/test/java/akka/stream/javadsl/AttributesTest.java +++ b/akka-stream-tests/src/test/java/akka/stream/javadsl/AttributesTest.java @@ -14,6 +14,7 @@ import org.junit.Test; import akka.stream.Attributes; import akka.stream.StreamTest; +import akka.testkit.AkkaJUnitActorSystemResource; import akka.testkit.AkkaSpec; public class AttributesTest extends StreamTest { diff --git a/akka-stream-tests/src/test/java/akka/stream/javadsl/BidiFlowTest.java b/akka-stream-tests/src/test/java/akka/stream/javadsl/BidiFlowTest.java index 1c50f2422a..39819ee528 100644 --- a/akka-stream-tests/src/test/java/akka/stream/javadsl/BidiFlowTest.java +++ b/akka-stream-tests/src/test/java/akka/stream/javadsl/BidiFlowTest.java @@ -25,6 +25,7 @@ import akka.japi.function.*; import akka.util.ByteString; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertArrayEquals; +import akka.testkit.AkkaJUnitActorSystemResource; public class BidiFlowTest extends StreamTest { public BidiFlowTest() { diff --git a/akka-stream-tests/src/test/java/akka/stream/javadsl/FlowTest.java b/akka-stream-tests/src/test/java/akka/stream/javadsl/FlowTest.java index 9372320bc6..55589e9b1d 100644 --- a/akka-stream-tests/src/test/java/akka/stream/javadsl/FlowTest.java +++ b/akka-stream-tests/src/test/java/akka/stream/javadsl/FlowTest.java @@ -26,6 +26,7 @@ import scala.concurrent.Await; import scala.concurrent.Future; import scala.concurrent.duration.Duration; import scala.concurrent.duration.FiniteDuration; +import akka.testkit.AkkaJUnitActorSystemResource; import java.util.*; import java.util.concurrent.CompletableFuture; diff --git a/akka-stream-tests/src/test/java/akka/stream/javadsl/FlowThrottleTest.java b/akka-stream-tests/src/test/java/akka/stream/javadsl/FlowThrottleTest.java index 30d5bb39a8..445c04d4ce 100644 --- a/akka-stream-tests/src/test/java/akka/stream/javadsl/FlowThrottleTest.java +++ b/akka-stream-tests/src/test/java/akka/stream/javadsl/FlowThrottleTest.java @@ -16,6 +16,7 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; import java.util.concurrent.TimeUnit; import akka.testkit.AkkaSpec; +import akka.testkit.AkkaJUnitActorSystemResource; import static org.junit.Assert.assertEquals; diff --git a/akka-stream-tests/src/test/java/akka/stream/javadsl/FramingTest.java b/akka-stream-tests/src/test/java/akka/stream/javadsl/FramingTest.java index 60ca3956eb..f4450ccffd 100644 --- a/akka-stream-tests/src/test/java/akka/stream/javadsl/FramingTest.java +++ b/akka-stream-tests/src/test/java/akka/stream/javadsl/FramingTest.java @@ -9,6 +9,7 @@ import akka.testkit.AkkaSpec; import akka.util.ByteString; import org.junit.ClassRule; import org.junit.Test; +import akka.testkit.AkkaJUnitActorSystemResource; public class FramingTest extends StreamTest { public FramingTest() { diff --git a/akka-stream-tests/src/test/java/akka/stream/javadsl/GraphDSLTest.java b/akka-stream-tests/src/test/java/akka/stream/javadsl/GraphDSLTest.java index b0f66a8bd2..960c0a7fa7 100644 --- a/akka-stream-tests/src/test/java/akka/stream/javadsl/GraphDSLTest.java +++ b/akka-stream-tests/src/test/java/akka/stream/javadsl/GraphDSLTest.java @@ -14,6 +14,7 @@ import akka.japi.function.*; import akka.testkit.AkkaSpec; import akka.testkit.JavaTestKit; import akka.testkit.TestProbe; +import akka.testkit.AkkaJUnitActorSystemResource; import org.junit.ClassRule; import org.junit.Test; diff --git a/akka-stream-tests/src/test/java/akka/stream/javadsl/KillSwitchTest.java b/akka-stream-tests/src/test/java/akka/stream/javadsl/KillSwitchTest.java index a31705335c..4caab0f544 100644 --- a/akka-stream-tests/src/test/java/akka/stream/javadsl/KillSwitchTest.java +++ b/akka-stream-tests/src/test/java/akka/stream/javadsl/KillSwitchTest.java @@ -11,6 +11,7 @@ import org.junit.Test; import java.util.concurrent.CompletionStage; import java.util.concurrent.TimeUnit; +import akka.testkit.AkkaJUnitActorSystemResource; import static org.junit.Assert.*; diff --git a/akka-stream-tests/src/test/java/akka/stream/javadsl/SinkTest.java b/akka-stream-tests/src/test/java/akka/stream/javadsl/SinkTest.java index 29f9d914dc..48a9e0fde7 100644 --- a/akka-stream-tests/src/test/java/akka/stream/javadsl/SinkTest.java +++ b/akka-stream-tests/src/test/java/akka/stream/javadsl/SinkTest.java @@ -24,6 +24,7 @@ import scala.concurrent.duration.Duration; import akka.japi.function.Function2; import akka.testkit.AkkaSpec; import akka.testkit.JavaTestKit; +import akka.testkit.AkkaJUnitActorSystemResource; import static org.junit.Assert.*; diff --git a/akka-stream-tests/src/test/java/akka/stream/javadsl/SourceTest.java b/akka-stream-tests/src/test/java/akka/stream/javadsl/SourceTest.java index 35b7383516..390344b512 100644 --- a/akka-stream-tests/src/test/java/akka/stream/javadsl/SourceTest.java +++ b/akka-stream-tests/src/test/java/akka/stream/javadsl/SourceTest.java @@ -28,6 +28,7 @@ import scala.concurrent.Future; import scala.concurrent.duration.Duration; import scala.concurrent.duration.FiniteDuration; import scala.util.Try; +import akka.testkit.AkkaJUnitActorSystemResource; import java.util.*; import java.util.concurrent.CompletableFuture; diff --git a/akka-stream-tests/src/test/java/akka/stream/javadsl/TcpTest.java b/akka-stream-tests/src/test/java/akka/stream/javadsl/TcpTest.java index 524e95c18c..91a0eb4442 100644 --- a/akka-stream-tests/src/test/java/akka/stream/javadsl/TcpTest.java +++ b/akka-stream-tests/src/test/java/akka/stream/javadsl/TcpTest.java @@ -29,6 +29,7 @@ import akka.testkit.AkkaSpec; import akka.stream.testkit.TestUtils; import akka.util.ByteString; import akka.testkit.JavaTestKit; +import akka.testkit.AkkaJUnitActorSystemResource; public class TcpTest extends StreamTest { public TcpTest() { diff --git a/akka-stream-tests/src/test/java/akka/stream/stage/StageTest.java b/akka-stream-tests/src/test/java/akka/stream/stage/StageTest.java index ee5985325c..505cc65b35 100644 --- a/akka-stream-tests/src/test/java/akka/stream/stage/StageTest.java +++ b/akka-stream-tests/src/test/java/akka/stream/stage/StageTest.java @@ -5,7 +5,7 @@ package akka.stream.stage; import akka.NotUsed; import akka.stream.StreamTest; -import akka.stream.javadsl.AkkaJUnitActorSystemResource; +import akka.testkit.AkkaJUnitActorSystemResource; import akka.stream.javadsl.Sink; import akka.stream.javadsl.Source; import akka.testkit.AkkaSpec; diff --git a/akka-testkit/src/test/java/akka/testkit/AkkaJUnitActorSystemResource.java b/akka-testkit/src/test/java/akka/testkit/AkkaJUnitActorSystemResource.java index 0230005588..b796ae54a0 100644 --- a/akka-testkit/src/test/java/akka/testkit/AkkaJUnitActorSystemResource.java +++ b/akka-testkit/src/test/java/akka/testkit/AkkaJUnitActorSystemResource.java @@ -4,28 +4,44 @@ package akka.testkit; import akka.actor.ActorSystem; + import com.typesafe.config.Config; import org.junit.rules.ExternalResource; /** - * This is a resource for creating an actor system before test start and shut it down afterwards. + * This is a resource for creating an actor system before test start and shut it + * down afterwards. * * To use it on a class level add this to your test class: * - * @ClassRule + * + * @ClassRule * public static AkkaJUnitActorSystemResource actorSystemResource = * new AkkaJUnitActorSystemResource(name, config); * * private final ActorSystem system = actorSystemResource.getSystem(); + * * * - * To use it on a per test level add this to your test class: + * To use it on a per test level add this to your test class: * - * @Rule + * + * @Rule * public AkkaJUnitActorSystemResource actorSystemResource = * new AkkaJUnitActorSystemResource(name, config); * - * private final ActorSystem system = actorSystemResource.getSystem(); + * private ActorSystem system = null; + * + * @Before + * public void beforeEach() { + * system = actorSystemResource.getSystem(); + * } + * + * + * Note that it is important to not use getSystem from the + * constructor of the test, becuase some test runners may create an + * instance of the class without actually using it later, resulting in + * memory leaks because of not shutting down the actor system. */ public class AkkaJUnitActorSystemResource extends ExternalResource { @@ -49,7 +65,6 @@ public class AkkaJUnitActorSystemResource extends ExternalResource { public AkkaJUnitActorSystemResource(String name, Config config) { this.name = name; this.config = config; - system = createSystem(name, config); } public AkkaJUnitActorSystemResource(String name) { @@ -58,9 +73,6 @@ public class AkkaJUnitActorSystemResource extends ExternalResource { @Override protected void before() throws Throwable { - // Sometimes the ExternalResource seems to be reused, and - // we don't run the constructor again, so if that's the case - // then create the system here if (system == null) { system = createSystem(name, config); } @@ -73,6 +85,9 @@ public class AkkaJUnitActorSystemResource extends ExternalResource { } public ActorSystem getSystem() { + if (system == null) { + system = createSystem(name, config); + } return system; } } diff --git a/akka-typed/src/test/scala/akka/typed/TypedSpec.scala b/akka-typed/src/test/scala/akka/typed/TypedSpec.scala index 760af3d57b..c7aa92fd44 100644 --- a/akka-typed/src/test/scala/akka/typed/TypedSpec.scala +++ b/akka-typed/src/test/scala/akka/typed/TypedSpec.scala @@ -25,13 +25,13 @@ import org.scalactic.Constraint /** * Helper class for writing tests for typed Actors with ScalaTest. */ -class TypedSpec(config: Config) extends Spec with Matchers with BeforeAndAfterAll with ScalaFutures with ConversionCheckedTripleEquals { +abstract class TypedSpec(config: Config) extends Spec with Matchers with BeforeAndAfterAll with ScalaFutures with ConversionCheckedTripleEquals { import TypedSpec._ import AskPattern._ def this() = this(ConfigFactory.empty) - implicit val system = ActorSystem(AkkaSpec.getCallerName(classOf[TypedSpec]), Props(guardian()), Some(config withFallback AkkaSpec.testConf)) + implicit val system = ActorSystem(TypedSpec.getCallerName(classOf[TypedSpec]), Props(guardian()), Some(config withFallback AkkaSpec.testConf)) implicit val timeout = Timeout(1.minute) implicit val patience = PatienceConfig(3.seconds) @@ -146,4 +146,14 @@ object TypedSpec { c.replyTo ! ctx.spawn(c.props, c.name) Same } + + def getCallerName(clazz: Class[_]): String = { + val s = (Thread.currentThread.getStackTrace map (_.getClassName) drop 1) + .dropWhile(_ matches "(java.lang.Thread|.*TypedSpec.?$)") + val reduced = s.lastIndexWhere(_ == clazz.getName) match { + case -1 ⇒ s + case z ⇒ s drop (z + 1) + } + reduced.head.replaceFirst(""".*\.""", "").replaceAll("[^a-zA-Z_0-9]", "_") + } }