Skip to content

Commit

Permalink
Merge pull request #1 from eed3si9n/wip/fix-parsing-id
Browse files Browse the repository at this point in the history
Add test case for number id in JSON-RPC
  • Loading branch information
tiqwab committed Mar 17, 2018
2 parents a5119a4 + e5d2588 commit 9d01bdf
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions sbt/src/test/scala/sbt/ServerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package sbt

import org.scalatest._
import scala.concurrent._
import scala.annotation.tailrec
import java.io.{ InputStream, OutputStream }
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.{ ThreadFactory, ThreadPoolExecutor }
Expand All @@ -23,15 +24,22 @@ class ServerSpec extends AsyncFlatSpec with Matchers {
"""{ "jsonrpc": "2.0", "id": 3, "method": "sbt/setting", "params": { "setting": "root/name" } }""",
out)
Thread.sleep(100)
val l2 = contentLength(in)
println(l2)
readLine(in)
readLine(in)
val x2 = readContentLength(in, l2)
println(x2)
assert(1 == 1)
assert(waitFor(in, 10) { s =>
s contains """"id":3"""
})
}
}

@tailrec
private[this] def waitFor(in: InputStream, num: Int)(f: String => Boolean): Boolean = {
if (num < 0) false
else
readFrame(in) match {
case Some(x) if f(x) => true
case _ =>
waitFor(in, num - 1)(f)
}
}
}

object ServerSpec {
Expand Down Expand Up @@ -90,6 +98,13 @@ object ServerSpec {
writeLine(message, out)
}

def readFrame(in: InputStream): Option[String] = {
val l = contentLength(in)
readLine(in)
readLine(in)
readContentLength(in, l)
}

def contentLength(in: InputStream): Int = {
readLine(in) map { line =>
line.drop(16).toInt
Expand Down

0 comments on commit 9d01bdf

Please sign in to comment.