GJL commented on a change in pull request #11683: [FLINK-15347] Add 
SupervisorActor which monitors the proper termination of AkkaRpcActors
URL: https://github.com/apache/flink/pull/11683#discussion_r409701035
 
 

 ##########
 File path: 
flink-runtime/src/test/java/org/apache/flink/runtime/rpc/akka/SupervisorActorTest.java
 ##########
 @@ -0,0 +1,268 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.runtime.rpc.akka;
+
+import org.apache.flink.util.ExceptionUtils;
+import org.apache.flink.util.FlinkException;
+import org.apache.flink.util.TestLogger;
+
+import akka.actor.AbstractActor;
+import akka.actor.ActorRef;
+import akka.actor.ActorSystem;
+import akka.actor.Props;
+import akka.actor.Terminated;
+import akka.japi.pf.ReceiveBuilder;
+import org.junit.Rule;
+import org.junit.Test;
+
+import javax.annotation.Nullable;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+
+public class SupervisorActorTest extends TestLogger {
+
+       @Rule
+       public final ActorSystemResource actorSystemResource = 
ActorSystemResource.defaultConfiguration();
+
+       @Test
+       public void completesTerminationFutureIfActorStops() {
+               final ActorSystem actorSystem = 
actorSystemResource.getActorSystem();
+
+               final ActorRef supervisor = 
SupervisorActor.startSupervisorActor(actorSystem);
+
+               final SupervisorActor.StartAkkaRpcActorResponse startResponse = 
SupervisorActor.startAkkaRpcActor(
+                       supervisor,
+                       terminationFuture -> Props.create(SimpleActor.class, 
terminationFuture),
+                       "foobar");
+
+               final SupervisorActor.ActorRegistration actorRegistration = 
startResponse.orElseThrow(cause -> new AssertionError("Expected the start to 
succeed.", cause));
+
+               final CompletableFuture<Void> terminationFuture = 
actorRegistration.getTerminationFuture();
+               assertThat(terminationFuture.isDone(), is(false));
+
+               
actorRegistration.getActorRef().tell(TerminateWithFutureCompletion.normal(), 
ActorRef.noSender());
+
+               terminationFuture.join();
+       }
+
+       @Test
+       public void 
completesTerminationFutureExceptionallyIfActorStopsExceptionally() throws 
Exception {
+               final ActorSystem actorSystem = 
actorSystemResource.getActorSystem();
+
+               final ActorRef supervisor = 
SupervisorActor.startSupervisorActor(actorSystem);
+
+               final SupervisorActor.StartAkkaRpcActorResponse startResponse = 
SupervisorActor.startAkkaRpcActor(
+                       supervisor,
+                       terminationFuture -> Props.create(SimpleActor.class, 
terminationFuture),
+                       "foobar");
+
+               final SupervisorActor.ActorRegistration actorRegistration = 
startResponse.orElseThrow(cause -> new AssertionError("Expected the start to 
succeed.", cause));
+
+               final CompletableFuture<Void> terminationFuture = 
actorRegistration.getTerminationFuture();
+               assertThat(terminationFuture.isDone(), is(false));
+
+               final FlinkException cause = new FlinkException("Test cause.");
+               
actorRegistration.getActorRef().tell(TerminateWithFutureCompletion.exceptionally(cause),
 ActorRef.noSender());
+
+               try {
+                       terminationFuture.get();
+                       fail("Expected the termination future being completed 
exceptionally");
+               } catch (ExecutionException expected) {
+                       final Throwable ignored = 
ExceptionUtils.findThrowable(expected, e -> e.equals(cause))
+                               .orElseThrow(() -> new 
FlinkException("Unexpected exception", expected));
+               }
+       }
+
+       @Test
+       public void 
completesTerminationFutureExceptionallyIfActorStopsWithoutReason() throws 
InterruptedException {
+               final ActorSystem actorSystem = 
actorSystemResource.getActorSystem();
+
+               final ActorRef supervisor = 
SupervisorActor.startSupervisorActor(actorSystem);
+
+               final SupervisorActor.StartAkkaRpcActorResponse startResponse = 
SupervisorActor.startAkkaRpcActor(
+                       supervisor,
+                       terminationFuture -> Props.create(SimpleActor.class, 
terminationFuture),
+                       "foobar");
+
+               final SupervisorActor.ActorRegistration actorRegistration = 
startResponse.orElseThrow(cause -> new AssertionError("Expected the start to 
succeed.", cause));
+
+               final CompletableFuture<Void> terminationFuture = 
actorRegistration.getTerminationFuture();
+               assertThat(terminationFuture.isDone(), is(false));
+
+               actorRegistration.getActorRef().tell(Terminate.INSTANCE, 
ActorRef.noSender());
+
+               try {
+                       terminationFuture.get();
+                       fail("Expected the termination future being completed 
exceptionally");
+               } catch (ExecutionException expected) {}
+       }
+
+       @Test
+       public void completesTerminationFutureExceptionallyIfActorFails() 
throws Exception {
+               final ActorSystem actorSystem = 
actorSystemResource.getActorSystem();
+
+               final ActorRef supervisor = 
SupervisorActor.startSupervisorActor(actorSystem);
+
+               final SupervisorActor.StartAkkaRpcActorResponse startResponse = 
SupervisorActor.startAkkaRpcActor(
+                       supervisor,
+                       terminationFuture -> Props.create(SimpleActor.class, 
terminationFuture),
+                       "foobar");
+
+               final SupervisorActor.ActorRegistration actorRegistration = 
startResponse.orElseThrow(cause -> new AssertionError("Expected the start to 
succeed.", cause));
+
+               final CompletableFuture<Void> terminationFuture = 
actorRegistration.getTerminationFuture();
+               assertThat(terminationFuture.isDone(), is(false));
+
+               final CompletableFuture<Terminated> 
actorSystemTerminationFuture = 
actorSystem.getWhenTerminated().toCompletableFuture();
+
+               final FlinkException cause = new FlinkException("Test cause.");
+               actorRegistration.getActorRef().tell(Fail.exceptionally(cause), 
ActorRef.noSender());
+
+               try {
+                       terminationFuture.get();
+                       fail("Expected the termination future being completed 
exceptionally");
+               } catch (ExecutionException expected) {
+                       final Throwable ignored = 
ExceptionUtils.findThrowable(expected, e -> e.equals(cause))
 
 Review comment:
   Why assign the throwable to `ignored` in the first place? Also in 
`completesTerminationFutureExceptionallyIfActorStopsExceptionally()`

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to