mirror of https://github.com/sbt/sbt.git
Update server tests to use shared json parser
This commit is contained in:
parent
d102c47415
commit
32f250fecb
|
|
@ -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"))
|
||||
}
|
||||
}
|
||||
|
|
@ -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 }
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue