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


##########
amber/src/test/scala/org/apache/texera/amber/engine/architecture/worker/WorkflowWorkerSpec.scala:
##########
@@ -0,0 +1,188 @@
+/*
+ * 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.architecture.worker
+
+import org.apache.pekko.actor.{ActorSystem, Props}
+import org.apache.pekko.testkit.{ImplicitSender, TestActorRef, TestKit, 
TestProbe}
+import org.apache.texera.amber.clustering.SingleNodeListener
+import org.apache.texera.amber.core.executor.OpExecWithClassName
+import org.apache.texera.amber.core.virtualidentity.{
+  ActorVirtualIdentity,
+  ChannelIdentity,
+  EmbeddedControlMessageIdentity
+}
+import 
org.apache.texera.amber.engine.architecture.coordinator.ReplayStatusUpdate
+import org.apache.texera.amber.engine.architecture.rpc.controlcommands.{
+  AsyncRPCContext,
+  ControlInvocation,
+  EmptyRequest,
+  InitializeExecutorRequest
+}
+import 
org.apache.texera.amber.engine.architecture.scheduling.config.WorkerConfig
+import org.apache.texera.amber.engine.architecture.worker.WorkflowWorker.{
+  ActorCommandElement,
+  DPInputQueueElement,
+  FaultToleranceConfig,
+  MainThreadDelegateMessage,
+  StateRestoreConfig,
+  TimerBasedControlElement,
+  TriggerSend,
+  WorkerReplayInitialization
+}
+import org.apache.texera.amber.engine.common.actormessage.Backpressure
+import org.apache.texera.amber.engine.common.ambermessage.WorkflowFIFOMessage
+import org.apache.texera.amber.engine.common.virtualidentity.util.COORDINATOR
+import org.apache.texera.amber.engine.common.{AmberRuntime, CheckpointState, 
SerializedState}
+import org.scalatest.BeforeAndAfterAll
+import org.scalatest.flatspec.AnyFlatSpecLike
+
+import java.net.URI
+import java.util.concurrent.{CompletableFuture, LinkedBlockingQueue}
+import scala.collection.mutable
+
+class WorkflowWorkerSpec
+    extends TestKit(ActorSystem("WorkflowWorkerSpec", 
AmberRuntime.pekkoConfig))
+    with ImplicitSender
+    with AnyFlatSpecLike
+    with BeforeAndAfterAll {
+
+  // The "Worker:...-<idx>" form is required so 
VirtualIdentityUtils.getWorkerIndex
+  // resolves an index; otherwise loadFromCheckpoint's restoreExecutorState 
throws.
+  private val identifier1 = ActorVirtualIdentity("Worker:WF1-E1-op-layer-1")
+
+  override def beforeAll(): Unit = {
+    system.actorOf(Props[SingleNodeListener](), "cluster-info")
+  }
+
+  override def afterAll(): Unit = {
+    TestKit.shutdownActorSystem(system)
+  }
+
+  private def mkWorker(): TestActorRef[WorkflowWorker] =
+    TestActorRef(
+      new WorkflowWorker(WorkerConfig(identifier1), 
WorkerReplayInitialization())
+    )
+
+  "WorkflowWorker" should "enqueue backpressure, run handleActorCommand 
directly, and preRestart" in {
+    val worker = mkWorker()
+    // Stop the DP thread first: it blocks on inputQueue.take and would 
otherwise
+    // steal the element we enqueue below before we can poll it.
+    worker.underlyingActor.dpThread.stop()
+
+    // (a) handleBackpressure enqueues an 
ActorCommandElement(Backpressure(true)).
+    worker.underlyingActor.handleBackpressure(true)
+    assert(worker.underlyingActor.inputQueue.poll() == 
ActorCommandElement(Backpressure(true)))
+
+    // (b) handleActorCommand is dead code (not wired into receive); invoke it 
directly
+    // and apply the returned PartialFunction.
+    val handler = worker.underlyingActor.handleActorCommand
+    assert(handler.isDefinedAt(Backpressure(true)))
+    handler.apply(Backpressure(true))
+
+    // (c) preRestart tears the worker down; safe because initState already 
ran (dpThread
+    // is non-null) during synchronous TestActorRef construction.
+    worker.underlyingActor.preRestart(new RuntimeException("x"), None)
+  }
+
+  "WorkflowWorker" should "run a MainThreadDelegateMessage closure via 
handleTriggerClosure" in {
+    val worker = mkWorker()
+    val f = new CompletableFuture[Boolean]()
+    // TestActorRef dispatches synchronously, so receive -> 
handleTriggerClosure runs inline.
+    worker ! MainThreadDelegateMessage(_ => f.complete(true))
+    assert(f.get())
+    worker.underlyingActor.dpThread.stop()

Review Comment:
   `CompletableFuture.get()` without a timeout can hang the test suite 
indefinitely if the message isn’t delivered (e.g., due to a future 
refactor/regression). Even if `TestActorRef` is synchronous today, a bounded 
wait keeps CI failures fast and debuggable.



##########
amber/src/test/scala/org/apache/texera/amber/engine/architecture/worker/WorkflowWorkerSpec.scala:
##########
@@ -0,0 +1,188 @@
+/*
+ * 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.architecture.worker
+
+import org.apache.pekko.actor.{ActorSystem, Props}
+import org.apache.pekko.testkit.{ImplicitSender, TestActorRef, TestKit, 
TestProbe}
+import org.apache.texera.amber.clustering.SingleNodeListener
+import org.apache.texera.amber.core.executor.OpExecWithClassName
+import org.apache.texera.amber.core.virtualidentity.{
+  ActorVirtualIdentity,
+  ChannelIdentity,
+  EmbeddedControlMessageIdentity
+}
+import 
org.apache.texera.amber.engine.architecture.coordinator.ReplayStatusUpdate
+import org.apache.texera.amber.engine.architecture.rpc.controlcommands.{
+  AsyncRPCContext,
+  ControlInvocation,
+  EmptyRequest,
+  InitializeExecutorRequest
+}
+import 
org.apache.texera.amber.engine.architecture.scheduling.config.WorkerConfig
+import org.apache.texera.amber.engine.architecture.worker.WorkflowWorker.{
+  ActorCommandElement,
+  DPInputQueueElement,
+  FaultToleranceConfig,
+  MainThreadDelegateMessage,
+  StateRestoreConfig,
+  TimerBasedControlElement,
+  TriggerSend,
+  WorkerReplayInitialization
+}
+import org.apache.texera.amber.engine.common.actormessage.Backpressure
+import org.apache.texera.amber.engine.common.ambermessage.WorkflowFIFOMessage
+import org.apache.texera.amber.engine.common.virtualidentity.util.COORDINATOR
+import org.apache.texera.amber.engine.common.{AmberRuntime, CheckpointState, 
SerializedState}
+import org.scalatest.BeforeAndAfterAll
+import org.scalatest.flatspec.AnyFlatSpecLike
+
+import java.net.URI
+import java.util.concurrent.{CompletableFuture, LinkedBlockingQueue}
+import scala.collection.mutable
+
+class WorkflowWorkerSpec
+    extends TestKit(ActorSystem("WorkflowWorkerSpec", 
AmberRuntime.pekkoConfig))
+    with ImplicitSender
+    with AnyFlatSpecLike
+    with BeforeAndAfterAll {
+
+  // The "Worker:...-<idx>" form is required so 
VirtualIdentityUtils.getWorkerIndex
+  // resolves an index; otherwise loadFromCheckpoint's restoreExecutorState 
throws.
+  private val identifier1 = ActorVirtualIdentity("Worker:WF1-E1-op-layer-1")
+
+  override def beforeAll(): Unit = {
+    system.actorOf(Props[SingleNodeListener](), "cluster-info")
+  }
+
+  override def afterAll(): Unit = {
+    TestKit.shutdownActorSystem(system)
+  }
+
+  private def mkWorker(): TestActorRef[WorkflowWorker] =
+    TestActorRef(
+      new WorkflowWorker(WorkerConfig(identifier1), 
WorkerReplayInitialization())
+    )
+
+  "WorkflowWorker" should "enqueue backpressure, run handleActorCommand 
directly, and preRestart" in {
+    val worker = mkWorker()
+    // Stop the DP thread first: it blocks on inputQueue.take and would 
otherwise
+    // steal the element we enqueue below before we can poll it.
+    worker.underlyingActor.dpThread.stop()
+
+    // (a) handleBackpressure enqueues an 
ActorCommandElement(Backpressure(true)).
+    worker.underlyingActor.handleBackpressure(true)
+    assert(worker.underlyingActor.inputQueue.poll() == 
ActorCommandElement(Backpressure(true)))
+
+    // (b) handleActorCommand is dead code (not wired into receive); invoke it 
directly
+    // and apply the returned PartialFunction.
+    val handler = worker.underlyingActor.handleActorCommand
+    assert(handler.isDefinedAt(Backpressure(true)))
+    handler.apply(Backpressure(true))
+

Review Comment:
   Invoking `WorkflowWorker.handleActorCommand` will print to stdout 
(`println(c)` in production code), which adds noise to test output. It’s better 
to silence stdout around this call since the test is only exercising the code 
path for coverage.



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