Skip to content

Commit

Permalink
Add tests for outcome encoding roundtrips, encode in base64 for now
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz committed Jan 24, 2024
1 parent 4be608a commit 206de20
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package weaver
package framework
package test

object TestOutcomeEncodingTests
extends FunSuite {

private val anOutcome =
TestOutcomeNative("suite-1", "test-1", 10, "verbosity goes here")

roundtripTest("even, small doubles for the duration",
anOutcome.copy(durationMs = 42))

roundtripTest("bigger doubles for the duration",
anOutcome.copy(durationMs = 50000))

roundtripTest("uneven doubles for the duration",
anOutcome.copy(durationMs = 42.57))

roundtripTest(
"Int.MaxValue duration",
anOutcome.copy(durationMs = Int.MaxValue)
)
roundtripTest(
"Double.MaxValue duration",
anOutcome.copy(durationMs = Double.MaxValue)
)

private def roundtripTest(name: TestName, input: TestOutcomeNative)(implicit
loc: SourceLocation) = {

test(name.copy(name = "Roundtrip test - " + name.name)) {
val encoded = TestOutcomeNative.encode(input)
val decoded = TestOutcomeNative.decode(encoded)

assert.same(input, decoded)
}
}

}
44 changes: 23 additions & 21 deletions modules/framework/src-js-native/RunnerCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import cats.syntax.all._
import sbt.testing.{ EventHandler, Logger, Task, TaskDef }
import java.io.ByteArrayOutputStream
import java.io.DataOutputStream
import java.util.Base64

trait RunnerCompat[F[_]] { self: sbt.testing.Runner =>
protected val args: Array[String]
Expand All @@ -27,15 +28,8 @@ trait RunnerCompat[F[_]] { self: sbt.testing.Runner =>
private[weaver] val failedTests = ListBuffer.empty[(SuiteName, TestOutcome)]

def reportDone(out: TestOutcomeNative): Unit = {
val serialised = ReadWriter.writer { p =>
p.writeString(out.suiteName)
p.writeString(out.testName)
p.writeDouble(out.durationMs)
p.writeString(out.verboseFormatting)
()
}
channel match {
case Some(send) => send(serialised)
case Some(send) => send(TestOutcomeNative.encode(out))
case None => failedTests.append(TestOutcomeNative.rehydrate(out))
}
}
Expand Down Expand Up @@ -74,17 +68,7 @@ trait RunnerCompat[F[_]] { self: sbt.testing.Runner =>
}

override def receiveMessage(msg: String): Option[String] = {
val outcome = ReadWriter.reader(msg) { p =>
val suite = p.readString()
val test = p.readString()
val dur = p.readDouble()
val verb = p.readString()

new TestOutcomeNative(suiteName = suite,
testName = test,
durationMs = dur,
verboseFormatting = verb)
}
val outcome = TestOutcomeNative.decode(msg)
reportDone(outcome)
None
}
Expand Down Expand Up @@ -202,7 +186,7 @@ private[weaver] object ReadWriter {
}

def reader[A](s: String)(f: Reader => A) = {
val buf = ByteBuffer.wrap(s.getBytes)
val buf = ByteBuffer.wrap(Base64.getDecoder().decode(s.getBytes()))
f(new Reader(buf, 0))
}

Expand All @@ -213,7 +197,7 @@ private[weaver] object ReadWriter {
try {
f(new Writer(dos))

baos.toString()
Base64.getEncoder().encodeToString(baos.toByteArray())
} finally {
dos.close()
baos.close()
Expand Down Expand Up @@ -245,6 +229,24 @@ object TestOutcomeNative {
)
}

def encode(to: TestOutcomeNative): String = ReadWriter.writer { p =>
p.writeString(to.suiteName)
p.writeString(to.testName)
p.writeDouble(to.durationMs)
p.writeString(to.verboseFormatting)
}
def decode(s: String): TestOutcomeNative = ReadWriter.reader(s) { p =>
val suite = p.readString()
val test = p.readString()
val dur = p.readDouble()
val verb = p.readString()

new TestOutcomeNative(suiteName = suite,
testName = test,
durationMs = dur,
verboseFormatting = verb)
}

private case class DecodedOutcome(
testName: String,
dur: FiniteDuration,
Expand Down

0 comments on commit 206de20

Please sign in to comment.