From 32f250fecbb3d4a4bf14a1cf8561bc7c92e3af96 Mon Sep 17 00:00:00 2001 From: Ethan Atkins Date: Sun, 28 Jun 2020 10:39:44 -0700 Subject: [PATCH] Update server tests to use shared json parser --- server-test/src/test/scala/sbt/ReadJson.scala | 18 +++++++ .../src/test/scala/testpkg/ClientTest.scala | 7 +++ .../src/test/scala/testpkg/TestServer.scala | 47 ++----------------- 3 files changed, 28 insertions(+), 44 deletions(-) create mode 100644 server-test/src/test/scala/sbt/ReadJson.scala diff --git a/server-test/src/test/scala/sbt/ReadJson.scala b/server-test/src/test/scala/sbt/ReadJson.scala new file mode 100644 index 000000000..6b9433bcd --- /dev/null +++ b/server-test/src/test/scala/sbt/ReadJson.scala @@ -0,0 +1,18 @@ +/* + * sbt + * Copyright 2011 - 2018, Lightbend, Inc. + * Copyright 2008 - 2010, Mark Harrah + * Licensed under Apache License 2.0 (see LICENSE) + */ + +package sbt + +import java.util.concurrent.atomic.AtomicBoolean +import java.io.InputStream + +object ReadJson { + def apply(in: InputStream, running: AtomicBoolean): Option[String] = { + val bytes = sbt.internal.util.ReadJsonFromInputStream(in, running, None).toArray + Some(new String(bytes, "UTF-8")) + } +} diff --git a/server-test/src/test/scala/testpkg/ClientTest.scala b/server-test/src/test/scala/testpkg/ClientTest.scala index 6c55daad3..dc85e05ef 100644 --- a/server-test/src/test/scala/testpkg/ClientTest.scala +++ b/server-test/src/test/scala/testpkg/ClientTest.scala @@ -1,3 +1,10 @@ +/* + * sbt + * Copyright 2011 - 2018, Lightbend, Inc. + * Copyright 2008 - 2010, Mark Harrah + * Licensed under Apache License 2.0 (see LICENSE) + */ + package testpkg import java.io.{ InputStream, OutputStream, PrintStream } diff --git a/server-test/src/test/scala/testpkg/TestServer.scala b/server-test/src/test/scala/testpkg/TestServer.scala index de674aa57..01964f39f 100644 --- a/server-test/src/test/scala/testpkg/TestServer.scala +++ b/server-test/src/test/scala/testpkg/TestServer.scala @@ -10,6 +10,7 @@ package testpkg import java.io.{ File, IOException } import java.nio.file.Path import java.util.concurrent.TimeoutException +import java.util.concurrent.atomic.AtomicBoolean import verify._ import sbt.RunFromSourceMain @@ -170,8 +171,7 @@ case class TestServer( val readBuffer = new Array[Byte](40960) var buffer: Vector[Byte] = Vector.empty var bytesRead = 0 - private val delimiter: Byte = '\n'.toByte - private val RetByte = '\r'.toByte + val running = new AtomicBoolean(true) hostLog("fork to a new sbt instance") val process = RunFromSourceMain.fork(baseDirectory, scalaVersion, sbtVersion, classpath) @@ -257,17 +257,7 @@ case class TestServer( writeEndLine } - def readFrame: Future[Option[String]] = Future { - def getContentLength: Int = { - readLine map { line => - line.drop(16).toInt - } getOrElse (0) - } - val l = getContentLength - readLine - readLine - readContentLength(l) - } + def readFrame: Future[Option[String]] = Future(sbt.ReadJson(in, running)) final def waitForString(duration: FiniteDuration)(f: String => Boolean): Boolean = { val deadline = duration.fromNow @@ -300,35 +290,4 @@ case class TestServer( impl() } - def readLine: Option[String] = { - if (buffer.isEmpty) { - val bytesRead = in.read(readBuffer) - if (bytesRead > 0) { - buffer = buffer ++ readBuffer.toVector.take(bytesRead) - } - } - val delimPos = buffer.indexOf(delimiter) - if (delimPos > 0) { - val chunk0 = buffer.take(delimPos) - buffer = buffer.drop(delimPos + 1) - // remove \r at the end of line. - val chunk1 = if (chunk0.lastOption contains RetByte) chunk0.dropRight(1) else chunk0 - Some(new String(chunk1.toArray, "utf-8")) - } else None // no EOL yet, so skip this turn. - } - - def readContentLength(length: Int): Option[String] = { - if (buffer.isEmpty) { - val bytesRead = in.read(readBuffer) - if (bytesRead > 0) { - buffer = buffer ++ readBuffer.toVector.take(bytesRead) - } - } - if (length <= buffer.size) { - val chunk = buffer.take(length) - buffer = buffer.drop(length) - Some(new String(chunk.toArray, "utf-8")) - } else None // have not read enough yet, so skip this turn. - } - }