Copilot commented on code in PR #7954:
URL: https://github.com/apache/texera/pull/7954#discussion_r3850152052


##########
amber/src/test/scala/org/apache/texera/amber/engine/common/client/AmberClientSpec.scala:
##########
@@ -0,0 +1,324 @@
+/*
+ * 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.texera.amber.engine.common.client
+
+import com.twitter.util.{Await => TwitterAwait, Duration => TwitterDuration}
+import org.apache.pekko.actor.{ActorRef, ActorSystem, Address, 
UnhandledMessage}
+import org.apache.pekko.pattern.StatusReply.Ack
+import org.apache.pekko.testkit.{TestKit, TestProbe}
+import org.apache.texera.amber.core.virtualidentity.ChannelIdentity
+import org.apache.texera.amber.core.workflow.{PhysicalPlan, WorkflowContext}
+import 
org.apache.texera.amber.engine.architecture.common.WorkflowActor.{NetworkAck, 
NetworkMessage}
+import org.apache.texera.amber.engine.architecture.coordinator.{
+  CoordinatorConfig,
+  ExecutionStateUpdate,
+  WorkflowRecoveryStatus
+}
+import 
org.apache.texera.amber.engine.architecture.rpc.controlreturns.WorkflowAggregatedState
+import org.apache.texera.amber.engine.common.ambermessage.{
+  NotifyFailedNode,
+  WorkflowFIFOMessage,
+  WorkflowFIFOMessagePayload,
+  WorkflowRecoveryMessage
+}
+import org.apache.texera.amber.engine.common.virtualidentity.util.{CLIENT, 
COORDINATOR}
+import org.scalatest.BeforeAndAfterAll
+import org.scalatest.flatspec.AnyFlatSpecLike
+import org.scalatest.matchers.should.Matchers
+
+import java.util.concurrent.atomic.AtomicInteger
+import java.util.concurrent.{ConcurrentLinkedQueue, CountDownLatch, TimeUnit}
+import scala.concurrent.Await
+import scala.concurrent.duration.DurationInt
+import scala.jdk.CollectionConverters._
+
+/**
+  * Unit test for [[AmberClient]].
+  *
+  * This spec lives in `...engine.common.client` on purpose: `ClientActor` and 
its
+  * companion messages are `private[client]`, so the event-delivery tests 
below could
+  * not be written from any other package.
+  *
+  * Every client is built over the empty plan
+  * (`PhysicalPlan(Set.empty, Set.empty)` + `CoordinatorConfig(None, None, 
None, None)`),
+  * the recipe three `org.apache.texera.web.service` specs already use: the 
constructor
+  * blocks on an `InitializeRequest`, which spawns a real `Coordinator` child, 
and only
+  * an empty plan makes that complete without an engine. Clients are always 
shut down
+  * in a `finally` -- amber's suites share one serially-run JVM, so a leaked 
client
+  * would leave live actors behind for every later suite.
+  */
+class AmberClientSpec
+    extends TestKit(ActorSystem("AmberClientSpec"))
+    with AnyFlatSpecLike
+    with Matchers
+    with BeforeAndAfterAll {
+
+  override def afterAll(): Unit = {
+    try TestKit.shutdownActorSystem(system)
+    finally super.afterAll()
+  }
+
+  private val failedNode = Address("pekko", "AmberClientSpec", "127.0.0.1", 
2552)
+
+  /**
+    * `AmberClient` declares `implicit val timeout = Timeout(1.minute)` for 
the ask in
+    * `notifyNodeFailure`, so a reply that never arrives would stall the suite 
for a
+    * full minute before ScalaTest reports an unnamed failure. Every await 
below uses
+    * this short explicit bound instead.
+    */
+  private val awaitTimeout: TwitterDuration = TwitterDuration.fromSeconds(5)
+
+  private def newClient(
+      actorSystem: ActorSystem = system,
+      errorHandler: Throwable => Unit = _ => ()
+  ): AmberClient =
+    new AmberClient(
+      actorSystem,
+      new WorkflowContext(),
+      PhysicalPlan(Set.empty, Set.empty),
+      CoordinatorConfig(None, None, None, None),
+      errorHandler
+    )
+
+  private def withClient(body: AmberClient => Unit): Unit = {
+    val client = newClient()
+    try body(client)
+    finally client.shutdown()
+  }
+
+  // 
---------------------------------------------------------------------------
+  // notifyNodeFailure
+  // 
---------------------------------------------------------------------------
+
+  "notifyNodeFailure" should "forward the failed node's address to the 
coordinator and complete with Ack" in {
+    // `ClientActor` replies Ack to ANY WorkflowRecoveryMessage, so the Ack on 
its own
+    // says nothing about what was sent. The `Coordinator` it forwards to has 
no arm
+    // for WorkflowRecoveryMessage (`WorkflowActor.receive` is a fixed 
orElse-chain
+    // with no catch-all), so pekko republishes the forwarded message verbatim 
on the
+    // event stream -- which is how the address can be observed from outside a 
client
+    // whose `clientActor` field is class-private.
+    val unhandled = TestProbe()
+    system.eventStream.subscribe(unhandled.ref, classOf[UnhandledMessage])
+    try {
+      withClient { client =>
+        TwitterAwait.result(client.notifyNodeFailure(failedNode), 
awaitTimeout) shouldBe Ack
+
+        val forwarded = 
unhandled.fishForSpecificMessage[UnhandledMessage](10.seconds) {
+          case msg @ UnhandledMessage(_: WorkflowRecoveryMessage, _, _) => msg
+        }
+        forwarded.message shouldBe WorkflowRecoveryMessage(CLIENT, 
NotifyFailedNode(failedNode))
+      }
+    } finally system.eventStream.unsubscribe(unhandled.ref)
+  }
+
+  it should "return an already-satisfied unit future once the client has been 
shut down" in {
+    val client = newClient()
+    client.shutdown()
+
+    val result = client.notifyNodeFailure(failedNode)
+
+    // Short-circuited, not asked: the actor took a PoisonPill in 
`shutdown()`, so an
+    // ask would sit unanswered until the one-minute timeout instead of 
resolving now.
+    result.isDefined shouldBe true
+    TwitterAwait.result(result, awaitTimeout) shouldBe ((): Unit)

Review Comment:
   The `result.isDefined shouldBe true` assertion makes this test depend on the 
internal completion timing of a Twitter `Future` rather than the behavior that 
matters (that the call completes quickly and does not perform an `ask`). This 
can be flaky if `Future[Any](())` is not guaranteed to be already satisfied 
immediately in all implementations/versions.
   
   Rely on the bounded `Await` only, which already fails fast if 
`notifyNodeFailure` accidentally performs the 1-minute `ask`.



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to