Copilot commented on code in PR #6722: URL: https://github.com/apache/texera/pull/6722#discussion_r3626593444
########## common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/source/sql/asterixdb/AsterixDBConnUtilSpec.scala: ########## @@ -0,0 +1,469 @@ +/* + * 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.operator.source.sql.asterixdb + +import com.sun.net.httpserver.{HttpExchange, HttpServer} +import kong.unirest.json.JSONException +import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach} +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers + +import java.net.{InetSocketAddress, URLDecoder} +import java.nio.charset.StandardCharsets +import java.util.concurrent.atomic.AtomicInteger +import scala.collection.mutable + +/** + * Characterization tests for AsterixDBConnUtil. Unlike the JDBC ConnUtils, this + * util talks plain HTTP via Unirest, so it is exercised end-to-end against an + * in-process HTTP stub server (same approach as LiteLLMProxyAuthSpec) standing + * in for an AsterixDB instance. No network dependency; the server binds port 0 + * to pick any free ephemeral port. + */ +class AsterixDBConnUtilSpec + extends AnyFlatSpec + with Matchers + with BeforeAndAfterAll + with BeforeAndAfterEach { + + // --------------------------------------------------------------------------- + // In-process AsterixDB stub + // --------------------------------------------------------------------------- + + private val versionHits = new AtomicInteger(0) + @volatile private var versionStatus: Int = 200 + @volatile private var versionBody: String = versionJson("0.9.9") + // Responds to POST /query/service based on the submitted statement. + @volatile private var queryResponder: String => (Int, String) = + _ => (200, """{"results":[]}""") + // Decoded form fields of every /query/service request, in arrival order. + private val recordedQueries = mutable.Buffer[Map[String, String]]() + + private val server: HttpServer = HttpServer.create(new InetSocketAddress(0), 0) + server.createContext( + "/admin/version", + (exchange: HttpExchange) => { + versionHits.incrementAndGet() + respond(exchange, versionStatus, versionBody) + } + ) + server.createContext( + "/query/service", + (exchange: HttpExchange) => { + val body = new String(exchange.getRequestBody.readAllBytes(), StandardCharsets.UTF_8) + val form = parseForm(body) Review Comment: The stub server reads the request body via exchange.getRequestBody.readAllBytes() but never closes the request InputStream. HttpExchange request bodies should be closed to avoid resource leaks (which can show up as flaky tests or hanging servers in larger suites). -- 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]
