kunwp1 commented on code in PR #9510: URL: https://github.com/apache/texera/pull/9510#discussion_r4595934920
########## amber/src/test/scala/org/apache/texera/amber/engine/e2e/RetryingDatabaseE2ESpec.scala: ########## @@ -0,0 +1,115 @@ +/* + * 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.e2e + +import com.twitter.util.Duration +import com.typesafe.scalalogging.Logger +import org.apache.pekko.actor.{ActorSystem, Props} +import org.apache.pekko.testkit.{ImplicitSender, TestKit} +import org.apache.pekko.util.Timeout +import org.apache.texera.amber.clustering.SingleNodeListener +import org.apache.texera.amber.core.workflow.PortIdentity +import org.apache.texera.amber.engine.common.AmberRuntime +import org.apache.texera.amber.engine.e2e.TestUtils.{ + cleanupWorkflowExecutionData, + initiateTexeraDBForTestCases, + setUpWorkflowExecutionData +} +import org.apache.texera.amber.operator.TestOperators +import org.apache.texera.amber.operator.source.scan.text.TextInputSourceOpDesc +import org.apache.texera.common.compiler.model.LogicalLink +import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach, Outcome, Retries} +import org.scalatest.flatspec.AnyFlatSpecLike + +import scala.concurrent.duration.DurationInt + +/** + * Shared retrying TestKit + database setup/teardown fixture for Amber E2E suites. + * Each subclass passes a unique `specId` so concurrent Iceberg and DB state stays isolated. + */ +abstract class RetryingDatabaseE2ESpec( + actorSystemName: String, + protected val specId: Int +) extends TestKit(ActorSystem(actorSystemName, AmberRuntime.pekkoConfig)) + with ImplicitSender + with AnyFlatSpecLike + with BeforeAndAfterAll + with BeforeAndAfterEach + with Retries { Review Comment: `LoopIntegrationSpec` still carries this exact setup (retries, `specId = 6`, and the DB and listener lifecycle), so it could extend this too. A few other specs also still keep their own copies of the new fixtures: - the `AmberClient` doubles in `ExecutionStatsServiceSpec`, `ExecutionResultServiceSpec` and `ExecutionConsoleServiceSpec` - the coordinator setup in `RetrieveWorkflowStateHandlerSpec` and `EmbeddedControlMessageHandlerSpec` Those could be a follow-up. ########## amber/src/test/scala/org/apache/texera/web/service/RecordingExecutionClient.scala: ########## @@ -0,0 +1,117 @@ +/* + * 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.web.service + +import com.twitter.util.Future +import io.reactivex.rxjava3.disposables.Disposable +import org.apache.pekko.actor.ActorSystem +import org.apache.texera.amber.core.workflow.{PhysicalPlan, WorkflowContext} +import org.apache.texera.amber.engine.architecture.coordinator.CoordinatorConfig +import org.apache.texera.amber.engine.architecture.rpc.controlreturns.EmptyReturn +import org.apache.texera.amber.engine.architecture.rpc.coordinatorservice.CoordinatorServiceFs2Grpc +import org.apache.texera.amber.engine.common.client.AmberClient + +import java.lang.reflect.{InvocationHandler, Method, Proxy} +import scala.collection.mutable +import scala.reflect.ClassTag + +/** + * Empty-plan `AmberClient` for execution-service unit specs: captures registered callbacks, + * honors subscription disposal, and optionally records coordinator RPC invocations. + */ +class RecordingExecutionClient( + system: ActorSystem, + customCoordinator: Option[CoordinatorServiceFs2Grpc[Future, Unit]] = None +) extends AmberClient( + system, + new WorkflowContext(), + PhysicalPlan(Set.empty, Set.empty), + CoordinatorConfig(None, None, None, None), + _ => () + ) { + + var shutdownCount = 0 + private val callbacks = mutable.Map.empty[Class[_], Any => Unit] + + /** `(method name, request)` of every call made through `coordinatorInterface` when no mock is provided. */ + val coordinatorCalls: mutable.ArrayBuffer[(String, Any)] = mutable.ArrayBuffer.empty + + /** Event class of every `Disposable` handed out by `registerCallback` that was later disposed. */ + val disposedCallbacks: mutable.ArrayBuffer[Class[_]] = mutable.ArrayBuffer.empty + + override val coordinatorInterface: CoordinatorServiceFs2Grpc[Future, Unit] = + customCoordinator.getOrElse( + Proxy + .newProxyInstance( + classOf[CoordinatorServiceFs2Grpc[Future, Unit]].getClassLoader, + Array(classOf[CoordinatorServiceFs2Grpc[Future, Unit]]), + new InvocationHandler { + override def invoke(proxy: Any, method: Method, args: Array[AnyRef]): AnyRef = { + if (args == null || args.isEmpty) { + if (method.getName == "hashCode") Integer.valueOf(System.identityHashCode(proxy)) + else if (method.getName == "toString") "TestCoordinatorInterface" + else null + } else { + coordinatorCalls += ((method.getName, args(0))) + Future.value(EmptyReturn()) + } + } + } + ) + .asInstanceOf[CoordinatorServiceFs2Grpc[Future, Unit]] + ) + + override def shutdown(): Unit = { + shutdownCount += 1 + super.shutdown() + } + + override def registerCallback[T](callback: T => Unit)(implicit ct: ClassTag[T]): Disposable = { + val clazz = ct.runtimeClass + callbacks(clazz) = callback.asInstanceOf[Any => Unit] + Disposable.fromAction(() => { + callbacks.remove(clazz) + disposedCallbacks += clazz + }) + } Review Comment: `callbacks` keeps one callback per event class. A second registration replaces the first, and disposing either one removes whatever is left. That matched `ExecutionReconfigurationServiceSpec`, but `WorkflowExecutionServiceSpec` now hands this client to the real services, and Runtime, Stats and Console all register `ExecutionStateUpdate` on it. Keeping a list per class makes it behave like the real client: ```scala private val callbacks = mutable.Map.empty[Class[_], mutable.ArrayBuffer[Any => Unit]] ... callbacks.getOrElseUpdate(clazz, mutable.ArrayBuffer.empty) += registered Disposable.fromAction(() => { callbacks.get(clazz).foreach { registrations => registrations -= registered if (registrations.isEmpty) callbacks.remove(clazz) } disposedCallbacks += clazz }) ``` `fire` would then deliver to each registration. ########## amber/src/test/scala/org/apache/texera/amber/engine/common/AmberSerdeTestSupport.scala: ########## @@ -0,0 +1,107 @@ +/* + * 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 + +import org.apache.pekko.actor.ActorSystem +import org.apache.pekko.serialization.{Serialization, SerializationExtension} +import org.apache.pekko.testkit.TestKit +import org.scalatest.{BeforeAndAfterAll, Suite} + +import java.nio.file.{FileSystemException, Files, Path} + +/** + * Mixin for specs that drive [[AmberRuntime.serde]] with a suite-local Pekko + * `ActorSystem`, plus best-effort recursive temp-directory cleanup. + */ +trait AmberSerdeTestSupport extends BeforeAndAfterAll { this: Suite => + + protected def amberSystemName: String = s"${getClass.getSimpleName}-test" + protected def lazyAmberActorSystem: Boolean = false + protected def restorePriorAmberRuntime: Boolean = false + + protected lazy val testSystem: ActorSystem = + ActorSystem(amberSystemName, AmberRuntime.pekkoConfig) + protected lazy val testSerde: Serialization = SerializationExtension(testSystem) + + if (!lazyAmberActorSystem) { + val _ = testSerde + } Review Comment: Could both flags go? `beforeAll` touches `testSystem` anyway, so `lazyAmberActorSystem` only decides whether the system is built at construction. With the eager default, a suite whose tests are all filtered out (as in the integration job) builds a system that `afterAll` never shuts down. Always restoring the previous runtime is also the safer default when suites share a JVM, and the three specs that set these to `true` would no longer need overrides. `deleteRecursively` and its two flags, each used by one spec, could go too. commons-io is already a dependency, and `FileUtils.deleteQuietly(dir.toFile)` does the same best-effort recursive delete. A spec that only needs cleanup then doesn't have to mix in an ActorSystem. -- 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]
