Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
Use log4s in test (JVM/JS) and log test exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
lefou committed Dec 3, 2018
1 parent 7e777d6 commit 6172292
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import java.util.UUID

import scala.reflect.{ClassTag, classTag}
import scala.util.Success
import scala.util.control.NonFatal

import blended.updater.config.json.PrickleProtocol._
import org.scalacheck.Arbitrary
Expand All @@ -13,8 +14,10 @@ import prickle._

class PrickleSpec extends FreeSpec with Matchers with PropertyChecks {

private[this] val log = org.log4s.getLogger

"Prickle real world test cases" - {
"1. deserialize a container info" in {
"1. deserialize a container info" in logException {

val svcInfos = List(
ServiceInfo(
Expand Down Expand Up @@ -86,7 +89,7 @@ class PrickleSpec extends FreeSpec with Matchers with PropertyChecks {
}

"Prickle should (de)serialize" - {
"an ActivateProfile" in {
"an ActivateProfile" in logException {

val overlay = OverlayRef("myOverlay", "1.0")
val action = ActivateProfile(UUID.randomUUID().toString(), profileName = "test", profileVersion = "1.0", overlays = Set(overlay))
Expand All @@ -105,7 +108,7 @@ class PrickleSpec extends FreeSpec with Matchers with PropertyChecks {

}

"an ActivateProfile as UpdateAction" in {
"an ActivateProfile as UpdateAction" in logException {

val overlay = OverlayRef("myOverlay", "1.0")
val action = ActivateProfile(UUID.randomUUID().toString(), profileName = "test", profileVersion = "1.0", overlays = Set(overlay))
Expand All @@ -124,7 +127,7 @@ class PrickleSpec extends FreeSpec with Matchers with PropertyChecks {
activate.overlays should be(Set(overlay))
}

"a GeneratedConfig" in {
"a GeneratedConfig" in logException {

val cfg = GeneratedConfig("filename", "{ key1: value1 }")
val json = Pickle.intoString(cfg)
Expand All @@ -133,7 +136,7 @@ class PrickleSpec extends FreeSpec with Matchers with PropertyChecks {

}

"a ServiceInfo" in {
"a ServiceInfo" in logException {

val svcInfo = ServiceInfo("mySvc", "myType", System.currentTimeMillis(), 1000l, Map("svc" -> "test"))

Expand All @@ -145,7 +148,7 @@ class PrickleSpec extends FreeSpec with Matchers with PropertyChecks {
svc should be(svcInfo)
}

"a list of ServiceInfo's" in {
"a list of ServiceInfo's" in logException {

val svcInfo = ServiceInfo("mySvc", "myType", System.currentTimeMillis(), 1000l, Map("svc" -> "test"))

Expand All @@ -157,7 +160,7 @@ class PrickleSpec extends FreeSpec with Matchers with PropertyChecks {
svcList should be(List(svcInfo))
}

"a ContainerInfo" in {
"a ContainerInfo" in logException {

val svcInfo = ServiceInfo("mySvc", "myType", System.currentTimeMillis(), 1000l, Map("svc" -> "test"))
val profile = Profile("myProfile", "1.0", OverlaySet(Set(), OverlayState.Valid, None))
Expand All @@ -176,7 +179,7 @@ class PrickleSpec extends FreeSpec with Matchers with PropertyChecks {
info2.profiles should be(List(profile))
}

"a ContainerRegistryResponseOK" in {
"a ContainerRegistryResponseOK" in logException {
val resp = ContainerRegistryResponseOK("response", List.empty)

val json = Pickle.intoString(resp)
Expand All @@ -187,7 +190,7 @@ class PrickleSpec extends FreeSpec with Matchers with PropertyChecks {

}

"a RemoteContainerState" in {
"a RemoteContainerState" in logException {
val svcInfo = ServiceInfo("mySvc", "myType", System.currentTimeMillis(), 1000l, Map("svc" -> "test"))
val profile = Profile("myProfile", "1.0", OverlaySet(Set(), OverlayState.Valid, None))

Expand Down Expand Up @@ -216,9 +219,11 @@ class PrickleSpec extends FreeSpec with Matchers with PropertyChecks {
arb: Arbitrary[T],
u: Unpickler[T],
p: Pickler[T]): Unit = {
classTag[T].runtimeClass.getSimpleName in {
classTag[T].runtimeClass.getSimpleName in logException {
forAll { d: T =>
assert(Unpickle[T].fromString(Pickle.intoString(d)) === Success(d))
val backAndForth = Unpickle[T].fromString(Pickle.intoString(d))
// log.info(s"data: [${backAndForth}]")
assert(backAndForth === Success(d))
}
}
}
Expand All @@ -243,9 +248,12 @@ class PrickleSpec extends FreeSpec with Matchers with PropertyChecks {
// testMapping(mapRemoteContainerState, unmapRemoteContainerState)
}

// As the test runs also in JS, we cannot use a SLF4J
def log = new {
def info(msg: => String) = println(msg)
def logException[T](f: => T): T = try {
f
} catch {
case NonFatal(e) =>
log.error(e)("Exception caught")
throw e
}

}
6 changes: 4 additions & 2 deletions project/BlendedUpdaterConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ object BlendedUpdaterConfigJs extends ProjectFactory {
libraryDependencies ++= Seq(
"com.github.benhutchison" %%% "prickle" % Dependencies.prickleVersion,
"org.scalatest" %%% "scalatest" % Dependencies.scalatestVersion % "test",
"org.scalacheck" %%% "scalacheck" % Dependencies.scalacheck.revision % "test"
"org.scalacheck" %%% "scalacheck" % Dependencies.scalacheck.revision % "test",
"org.log4s" %%% "log4s" % Dependencies.log4s.revision % "test"
)
)
)
Expand All @@ -45,7 +46,8 @@ object BlendedUpdaterConfigJvm extends ProjectFactory {
Dependencies.scalatest % "test",
Dependencies.logbackClassic % "test",
Dependencies.logbackCore % "test",
Dependencies.scalacheck % "test"
Dependencies.scalacheck % "test",
Dependencies.log4s % "test"
),
adaptBundle = b => b.copy(
exportPackage = Seq(
Expand Down
2 changes: 2 additions & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ object Dependencies {
val levelDbJava = "org.iq80.leveldb" % "leveldb" % "0.9"
val levelDbJni = "org.fusesource.leveldbjni" % "leveldbjni-all" % "1.8"
val liquibase = "org.liquibase" % "liquibase-core" % "3.6.1"
/** Only for use in test that also runs in JS */
val log4s = "org.log4s" %% "log4s" % "1.6.1"
val logbackCore = "ch.qos.logback" % "logback-core" % "1.2.3"
val logbackClassic = "ch.qos.logback" % "logback-classic" % "1.2.3"

Expand Down

0 comments on commit 6172292

Please sign in to comment.