vicennial commented on code in PR #40675: URL: https://github.com/apache/spark/pull/40675#discussion_r1174917132
########## connector/connect/client/jvm/src/test/scala/org/apache/spark/sql/application/ReplE2ESuite.scala: ########## @@ -0,0 +1,128 @@ +/* + * 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.spark.sql.application + +import java.io.{PipedInputStream, PipedOutputStream} +import java.util.concurrent.{Executors, Semaphore, TimeUnit} + +import org.apache.commons.io.output.ByteArrayOutputStream + +import org.apache.spark.sql.connect.client.util.RemoteSparkSession + +class ReplE2ESuite extends RemoteSparkSession { + + private val executorService = Executors.newSingleThreadExecutor() + private val TIMEOUT_SECONDS = 10 + + private var testSuiteOut: PipedOutputStream = _ + private var ammoniteOut: ByteArrayOutputStream = _ + private var errorStream: ByteArrayOutputStream = _ + private var ammoniteIn: PipedInputStream = _ + private val semaphore: Semaphore = new Semaphore(0) + + private def getCleanString(out: ByteArrayOutputStream): String = { + // Remove ANSI colour codes + // Regex taken from https://stackoverflow.com/a/25189932 + out.toString("UTF-8").replaceAll("\u001B\\[[\\d;]*[^\\d;]", "") + } + + override def beforeAll(): Unit = { + super.beforeAll() + ammoniteOut = new ByteArrayOutputStream() + testSuiteOut = new PipedOutputStream() + // Connect the `testSuiteOut` and `ammoniteIn` pipes + ammoniteIn = new PipedInputStream(testSuiteOut) + errorStream = new ByteArrayOutputStream() + + val args = Array("--port", serverPort.toString) + val task = new Runnable { + override def run(): Unit = { + ConnectRepl.doMain( + args = args, + semaphore = Some(semaphore), + inputStream = ammoniteIn, + outputStream = ammoniteOut, + errorStream = errorStream) + } + } + + executorService.submit(task) + } + + override def afterAll(): Unit = { + executorService.shutdownNow() + super.afterAll() + } + + def runCommandsInShell(input: String): String = { + require(input.nonEmpty) + // Pad the input with a semaphore release so that we know when the execution of the provided + // input is complete. + val paddedInput = input + '\n' + "semaphore.release()\n" + testSuiteOut.write(paddedInput.getBytes) + testSuiteOut.flush() + if (!semaphore.tryAcquire(TIMEOUT_SECONDS, TimeUnit.SECONDS)) { + val failOut = getCleanString(ammoniteOut) + val errOut = getCleanString(errorStream) + val errorString = + s""" + |REPL Timed out while running command: $input + |Console output: $failOut + |Error output: $errOut + |""".stripMargin + throw new RuntimeException(errorString) + } + getCleanString(ammoniteOut) + } + + def assertContains(message: String, output: String): Unit = { + val isContain = output.contains(message) + assert(isContain, "Ammonite output did not contain '" + message + "':\n" + output) + } + + test("Simple query") { Review Comment: Looking. The output looks a bit weird - The first tests fails due to a REPL timeout but the error logs indicate it is just setting up (albeit slowly). The first 'false' failure seems to cause a chain reaction of failures for the other tests because they're all looking at the shell output earlier then they should because of the semaphore permit left un-acquired from the first test. An increase in the repl shell timeout should fix this (+ draining permits after every test). Verifying -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
