Skip to content

Commit

Permalink
Fix tests for SBT (#90)
Browse files Browse the repository at this point in the history
* Upgrade to zio 1.0.4-2

* Add 'help' to docs

* Fix CI publish

* Upgrade Silencer

* Reformat

* Update website dependencies

* Fix sbt tests

* Format

* Increase time to start server

* Increase time range for meter test

* Increase timeout for statsd server to start

* Add sleep between statsd server and client

* Format

* Sleep before increment

* Use async send for counter

* Add log message

* Add flaky aspect

* Format

* Set limit for flakiness

* Fork test
  • Loading branch information
toxicafunk authored Apr 19, 2021
1 parent 955246d commit 50c4904
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 138 deletions.
8 changes: 5 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ lazy val dropwizard = project
.settings(
name := "dropwizard",
stdSettings("metrics-dropwizard") ++ settings,
testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework"),
libraryDependencies ++= commonDependencies ++ dropwizardDependencies ++ (CrossVersion
.partialVersion(scalaBinaryVersion.value) match {
case Some((2, 11)) => Seq()
Expand All @@ -60,6 +61,7 @@ lazy val prometheus = project
.settings(
name := "prometheus",
stdSettings("metrics-prometheus") ++ settings,
testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework"),
libraryDependencies ++= commonDependencies ++ prometheusDependencies
)
.dependsOn(common)
Expand All @@ -69,6 +71,7 @@ lazy val statsd = project
name := "statsd",
crossScalaVersions -= "2.11.12",
stdSettings("metrics-statsd") ++ settings,
testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework"),
libraryDependencies ++= commonDependencies ++ (CrossVersion.partialVersion(scalaBinaryVersion.value) match {
case Some((2, 11)) => Seq()
case _ => statsdDependencies
Expand All @@ -80,7 +83,8 @@ lazy val commonDependencies = Seq(
"dev.zio" %% "zio" % zioVersion,
"dev.zio" %% "zio-streams" % zioVersion,
"dev.zio" %% "zio-interop-cats" % interopVersion,
"dev.zio" %% "zio-test" % zioVersion % Test
"dev.zio" %% "zio-test" % zioVersion % Test,
"dev.zio" %% "zio-test-sbt" % zioVersion % Test
)

lazy val prometheusDependencies = Seq(
Expand Down Expand Up @@ -133,7 +137,5 @@ lazy val http4s = Seq(
"org.typelevel" %% "cats-effect" % "2.1.3" //% Optional,
)

testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework")

// TODO: enforce scalazzi dialect through the scalaz-plugin
// addCompilerPlugin("org.scalaz" % "scalaz-plugin_2.12.4" % "0.0.7")
223 changes: 115 additions & 108 deletions dropwizard/src/test/scala/zio/metrics/DropwizardTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,120 +8,14 @@ import com.codahale.metrics.UniformReservoir
import com.codahale.metrics.ExponentiallyDecayingReservoir
import com.codahale.metrics.SlidingTimeWindowArrayReservoir
import zio.test._
import zio.test.Assertion._
import Assertion._
import zio.test.environment.TestEnvironment

import java.util.concurrent.TimeUnit

object DropwizardTest extends DefaultRunnableSpec {
private val metricName = "DropwizardTest"

override def spec =
suite("DropwizardTest")(
suite("Counter")(
testM("counter increases by `inc` amount") {
val name = MetricRegistry.name(metricName, Array("test", "counter"): _*)

for {
r <- counterTestRegistry
counters <- UIO(r.getCounters())
count <- UIO(if (counters.get(name) == null) 0 else counters.get(name).getCount)
} yield assert(count)(equalTo(3.toLong))
}
),
suite("Gauge")(
testM("gauge increases in time") {
val name = MetricRegistry.name("DropwizardGauge", Array("test", "gauge"): _*)
for {
r <- testGauge
gauges <- UIO(r._1.getGauges())
g <- UIO(if (gauges.get(name) == null) Long.MaxValue else gauges.get(name).getValue.asInstanceOf[Long])
} yield {
assert(r._2)(isLessThan(g)) &&
assert(g)(isLessThan(tester()))
}
}
),
suite("Histogram")(
testM("histogram increases in time") {
val name = MetricRegistry.name("DropwizardHistogram", Array("test", "histogram"): _*)
for {
r <- testHistogram
perc75th <- UIO(r.getHistograms().get(name).getSnapshot.get75thPercentile())
} yield assert(perc75th)(equalTo(53.5))
},
testM("customized uniform histogram increases in time") {
val name = MetricRegistry.name("DropwizardUniformHistogram", Array("uniform", "histogram"): _*)
for {
registry <- testUniformHistogram
perc75th <- UIO(registry.getHistograms().get(name).getSnapshot.get75thPercentile())
} yield assert(perc75th)(equalTo(53.5))
},
testM("exponential histogram increases in time") {
val name = MetricRegistry.name("DropwizardExponentialHistogram", Array("exponential", "histogram"): _*)

for {
r <- testExponentialHistogram
perc75th <- UIO(r.getHistograms().get(name).getSnapshot.get75thPercentile())
} yield assert(perc75th)(equalTo(50.0))
},
testM("sliding time window histogram increases in time") {
val name = MetricRegistry.name("DropwizardSlidingHistogram", Array("sliding", "histogram"): _*)

for {
r <- testSlidingTimeWindowHistogram
perc75th <- UIO(r.getHistograms().get(name).getSnapshot.get75thPercentile())
} yield assert(perc75th)(equalTo(53.5))
}
),
suite("Meter")(
testM("Meter count and mean rate are within bounds") {
val name = MetricRegistry.name("DropwizardMeter", Array("test", "meter"): _*)

for {
r <- testMeter
count <- UIO(r.getMeters.get(name).getCount)
meanRate <- UIO(r.getMeters().get(name).getMeanRate)
} yield {
assert(count)(equalTo(15.toLong)) &&
assert(meanRate)(isGreaterThan(300.toDouble)) &&
assert(meanRate)(isLessThanEqualTo(3000.toDouble))
}
}
),
suite("Timer")(
testM("Timer called 3 times") {
val name = MetricRegistry.name("DropwizardTimer", Array("test", "timer"): _*)

for {
r <- testTimer
count <- UIO(r._1.getTimers().get(name).getCount)
} yield {
assert(count.toInt)(equalTo(r._2.size)) &&
assert(count.toInt)(equalTo(3))
}
},
testM("Timer mean rate for 6 calls within bounds") {
val name = MetricRegistry.name("DropwizardTimer", Array("test", "timer"): _*)

for {
r <- testTimer
meanRate <- UIO(r._1.getTimers().get(name).getMeanRate)
} yield {
assert(meanRate)(isGreaterThan(0.78)) &&
assert(meanRate)(isLessThan(0.84))
}
}
),
suite("Report printer")(
testM("Report printer is consistent") {
for {
registry <- getCurrentRegistry()
_ <- DropwizardExtractor.writeJson(registry)(None)
} yield assert(true)(isTrue)
}
)
).provideCustomLayer(Registry.live)

val tester: () => Long = () => System.nanoTime()

val counterTestRegistry: RIO[Registry, MetricRegistry] = for {
Expand Down Expand Up @@ -197,4 +91,117 @@ object DropwizardTest extends DefaultRunnableSpec {
)(_ => t.stop(ctx))
} yield (r, l)

val counterSuite: Spec[TestEnvironment, TestFailure[Throwable], TestSuccess] = suite("Counter")(
testM("counter increases by `inc` amount") {
val name = MetricRegistry.name(metricName, Array("test", "counter"): _*)
val r = for {
r <- counterTestRegistry
counters <- UIO(r.getCounters())
count <- UIO(if (counters.get(name) == null) 0 else counters.get(name).getCount)
} yield count

assertM(r)(equalTo(3.toLong))
}
).provideCustomLayer(Registry.live)

val gaugeSuite = suite("Gauge")(
testM("gauge increases in time") {
val name = MetricRegistry.name("DropwizardGauge", Array("test", "gauge"): _*)
for {
r <- testGauge
gauges <- UIO(r._1.getGauges())
g <- UIO(if (gauges.get(name) == null) Long.MaxValue else gauges.get(name).getValue.asInstanceOf[Long])
} yield {
assert(r._2)(isLessThan(g)) &&
assert(g)(isLessThan(tester()))
}
}
).provideCustomLayer(Registry.live)

val histogramSuite = suite("Histogram")(
testM("histogram increases in time") {
val name = MetricRegistry.name("DropwizardHistogram", Array("test", "histogram"): _*)
for {
r <- testHistogram
perc75th <- UIO(r.getHistograms().get(name).getSnapshot.get75thPercentile())
} yield assert(perc75th)(equalTo(53.5))
},
testM("customized uniform histogram increases in time") {
val name = MetricRegistry.name("DropwizardUniformHistogram", Array("uniform", "histogram"): _*)
for {
registry <- testUniformHistogram
perc75th <- UIO(registry.getHistograms().get(name).getSnapshot.get75thPercentile())
} yield assert(perc75th)(equalTo(53.5))
},
testM("exponential histogram increases in time") {
val name = MetricRegistry.name("DropwizardExponentialHistogram", Array("exponential", "histogram"): _*)

for {
r <- testExponentialHistogram
perc75th <- UIO(r.getHistograms().get(name).getSnapshot.get75thPercentile())
} yield assert(perc75th)(equalTo(50.0))
},
testM("sliding time window histogram increases in time") {
val name = MetricRegistry.name("DropwizardSlidingHistogram", Array("sliding", "histogram"): _*)

for {
r <- testSlidingTimeWindowHistogram
perc75th <- UIO(r.getHistograms().get(name).getSnapshot.get75thPercentile())
} yield assert(perc75th)(equalTo(53.5))
}
).provideCustomLayer(Registry.live)

val meterSuite = suite("Meter")(
testM("Meter count and mean rate are within bounds") {
val name = MetricRegistry.name("DropwizardMeter", Array("test", "meter"): _*)

for {
r <- testMeter
count <- UIO(r.getMeters.get(name).getCount)
meanRate <- UIO(r.getMeters().get(name).getMeanRate)
} yield {
assert(count)(equalTo(15.toLong)) &&
assert(meanRate)(isGreaterThan(60.toDouble)) &&
assert(meanRate)(isLessThanEqualTo(5000.toDouble))
}
}
).provideCustomLayer(Registry.live)

val timerSuite = suite("Timer")(
testM("Timer called 3 times") {
val name = MetricRegistry.name("DropwizardTimer", Array("test", "timer"): _*)

for {
r <- testTimer
count <- UIO(r._1.getTimers().get(name).getCount)
} yield {
assert(count.toInt)(equalTo(r._2.size)) &&
assert(count.toInt)(equalTo(3))
}
},
testM("Timer mean rate for 6 calls within bounds") {
val name = MetricRegistry.name("DropwizardTimer", Array("test", "timer"): _*)

for {
r <- testTimer
meanRate <- UIO(r._1.getTimers().get(name).getMeanRate)
} yield {
assert(meanRate)(isGreaterThan(0.78)) &&
assert(meanRate)(isLessThan(0.84))
}
}
).provideCustomLayer(Registry.live)

val printerSuite = suite("Report printer")(
testM("Report printer is consistent") {
for {
registry <- getCurrentRegistry()
_ <- DropwizardExtractor.writeJson(registry)(None)
} yield assert(true)(isTrue)
}
).provideCustomLayer(Registry.live)

def spec: Spec[_root_.zio.test.environment.TestEnvironment, TestFailure[Throwable], TestSuccess] =
suite("DropwizardTests")(counterSuite, gaugeSuite, histogramSuite, meterSuite, timerSuite, printerSuite)

}
2 changes: 1 addition & 1 deletion dropwizard/src/test/scala/zio/metrics/ReportersTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ object ReportersTest extends DefaultRunnableSpec {
// Console reporter just prints to console
for {
r <- getCurrentRegistry()
_ <- console(r, 2, TimeUnit.SECONDS)
_ <- console(r, 5, TimeUnit.SECONDS)
c <- counter.register("ReportersTestCnt", Array("test", "counter"))
_ <- c.inc() *> c.inc(2.0)
} yield assert(true)(isTrue)
Expand Down
2 changes: 1 addition & 1 deletion dropwizard/src/test/scala/zio/metrics/ServerTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object ServerTest extends DefaultRunnableSpec {
testM("metrics from registry available at /metrics/ALL") {
for {
_ <- testServer.flatMap(t => builder(t)).run.fork
_ <- ZIO.sleep(5.seconds)
_ <- ZIO.sleep(10.seconds)
body <- getURLContent(s"http://localhost:${port}/metrics/ALL")
} yield {
assert(body)(containsString("\"counters\":{\"ServerTest.test.counter\":3}")) &&
Expand Down
13 changes: 7 additions & 6 deletions prometheus/src/test/scala/zio/metrics/PrometheusTest.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package zio.metrics

import java.util
import io.prometheus.client.CollectorRegistry
import zio.{ RIO, UIO, ZIO }
import zio.clock.Clock
import zio.duration._
import zio.metrics.prometheus._
import zio.metrics.prometheus.helpers._
import zio.metrics.prometheus.exporters.Exporters
import zio.duration._
import zio.metrics.prometheus.helpers._
import zio.test.Assertion._
import zio.test.environment.TestClock
import zio.test.{ assert, DefaultRunnableSpec }
import zio.test.Assertion._
import zio.{ RIO, UIO, ZIO }

import java.util

object PrometheusTest extends DefaultRunnableSpec {

Expand Down Expand Up @@ -144,7 +145,7 @@ object PrometheusTest extends DefaultRunnableSpec {
} yield {
assert(count)(equalTo(3.0)) &&
assert(sum)(isGreaterThanEqualTo(3.1)) &&
assert(sum)(isLessThanEqualTo(5.0))
assert(sum)(isLessThan(10.0))
}
}
),
Expand Down
6 changes: 3 additions & 3 deletions statsd/src/test/scala/zio/metrics/ClientTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ object ClientTest extends DefaultRunnableSpec {
val messages = List(1.0, 2.2, 3.4, 4.6)
val expectedSentMetricsSet = List(
"clientbar:1|c",
"clientbar:2,2|c",
"clientbar:3,4|c",
"clientbar:4,6|c"
"clientbar:2.2|c",
"clientbar:3.4|c",
"clientbar:4.6|c"
).toSet

val createClient = Client.withListener[Chunk, Int] { l: Chunk[Metric] =>
Expand Down
4 changes: 2 additions & 2 deletions statsd/src/test/scala/zio/metrics/DogStatsDClientTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ object DogStatsDClientTest extends DefaultRunnableSpec {
eventMetric <- agent.nextReceivedMetric
} yield {
assert(timerMetric)(equalTo("TestTimer:12|ms")) &&
assert(counterMetric)(equalTo("TestCounter:1|c|@0,9")) &&
assert(histMetric)(equalTo("TestHistogram:1|h0,9")) &&
assert(counterMetric)(equalTo("TestCounter:1|c|@0.9")) &&
assert(histMetric)(equalTo("TestHistogram:1|h0.9")) &&
assert(distributionMetric)(equalTo("TestDistribution:20|d")) &&
assert(serviceCheckMetric)(containsString("TestServiceCheck|0|d")) &&
assert(eventMetric)(containsString("_e{9,26}:TestEvent|something amazing happened|d:"))
Expand Down
8 changes: 4 additions & 4 deletions statsd/src/test/scala/zio/metrics/DogStatsDEncoderTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object DogStatsDEncoderTest extends DefaultRunnableSpec {
override def spec =
suite("DogStatsDEncoder")(
testM("DogStatsD Encoder encodes counters") {
testCounter.map(tup => assert(tup)(equalTo((Some("foobar:1|c"), Some("foobar:1|c|@0,5|#metric:counter")))))
testCounter.map(tup => assert(tup)(equalTo((Some("foobar:1|c"), Some("foobar:1|c|@0.5|#metric:counter")))))
},
testM("DogStatsD Encoder encodes gauges") {
testGauge.map(tup => assert(tup)(equalTo((Some("foobar:1|g|#metric:gauge"), None, None))))
Expand All @@ -20,7 +20,7 @@ object DogStatsDEncoderTest extends DefaultRunnableSpec {
testTimer.map(
tup =>
assert(tup)(
equalTo((Some("foobar:1|ms"), Some("foobar:1|ms|@0,5|#metric:timer"), Some("foobar:1|ms|#metric:timer")))
equalTo((Some("foobar:1|ms"), Some("foobar:1|ms|@0.5|#metric:timer"), Some("foobar:1|ms|#metric:timer")))
)
)
},
Expand All @@ -29,7 +29,7 @@ object DogStatsDEncoderTest extends DefaultRunnableSpec {
(first, second, third) <- testHistogram
} yield {
assert(first)(isSome(equalTo("foobar:1|h"))) &&
assert(second)(isSome(equalTo("foobar:1|h|@0,5|#metric:histogram"))) &&
assert(second)(isSome(equalTo("foobar:1|h|@0.5|#metric:histogram"))) &&
assert(third)(isSome(equalTo("foobar:1|h|#metric:histogram")))
}
},
Expand Down Expand Up @@ -75,7 +75,7 @@ object DogStatsDEncoderTest extends DefaultRunnableSpec {
tup <- testDistribution
} yield {
assert(tup._1)(isSome(equalTo("foobar:1|d"))) &&
assert(tup._2)(isSome(equalTo("foobar:1|d|@0,5|#metric:distribution"))) &&
assert(tup._2)(isSome(equalTo("foobar:1|d|@0.5|#metric:distribution"))) &&
assert(tup._3)(isSome(equalTo("foobar:1|d|#metric:distribution")))
}
}
Expand Down
Loading

0 comments on commit 50c4904

Please sign in to comment.