From 50c49040a9b50e2a3def37ed911551b4606cc6f8 Mon Sep 17 00:00:00 2001 From: toxicafunk Date: Tue, 20 Apr 2021 01:20:14 +0200 Subject: [PATCH] Fix tests for SBT (#90) * 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 --- build.sbt | 8 +- .../scala/zio/metrics/DropwizardTest.scala | 223 +++++++++--------- .../scala/zio/metrics/ReportersTest.scala | 2 +- .../test/scala/zio/metrics/ServerTest.scala | 2 +- .../scala/zio/metrics/PrometheusTest.scala | 13 +- .../test/scala/zio/metrics/ClientTest.scala | 6 +- .../zio/metrics/DogStatsDClientTest.scala | 4 +- .../zio/metrics/DogStatsDEncoderTest.scala | 8 +- .../scala/zio/metrics/StatsDClientTest.scala | 28 ++- .../scala/zio/metrics/StatsDEncoderTest.scala | 2 +- 10 files changed, 158 insertions(+), 138 deletions(-) diff --git a/build.sbt b/build.sbt index 361a9c71..a0f30a0b 100644 --- a/build.sbt +++ b/build.sbt @@ -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() @@ -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) @@ -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 @@ -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( @@ -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") diff --git a/dropwizard/src/test/scala/zio/metrics/DropwizardTest.scala b/dropwizard/src/test/scala/zio/metrics/DropwizardTest.scala index e92c5b36..0b8de1a8 100644 --- a/dropwizard/src/test/scala/zio/metrics/DropwizardTest.scala +++ b/dropwizard/src/test/scala/zio/metrics/DropwizardTest.scala @@ -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 { @@ -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) + } diff --git a/dropwizard/src/test/scala/zio/metrics/ReportersTest.scala b/dropwizard/src/test/scala/zio/metrics/ReportersTest.scala index 50cdde82..dd98024b 100644 --- a/dropwizard/src/test/scala/zio/metrics/ReportersTest.scala +++ b/dropwizard/src/test/scala/zio/metrics/ReportersTest.scala @@ -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) diff --git a/dropwizard/src/test/scala/zio/metrics/ServerTest.scala b/dropwizard/src/test/scala/zio/metrics/ServerTest.scala index a5691de7..961892d9 100644 --- a/dropwizard/src/test/scala/zio/metrics/ServerTest.scala +++ b/dropwizard/src/test/scala/zio/metrics/ServerTest.scala @@ -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}")) && diff --git a/prometheus/src/test/scala/zio/metrics/PrometheusTest.scala b/prometheus/src/test/scala/zio/metrics/PrometheusTest.scala index c55fb83b..77a6d8b9 100644 --- a/prometheus/src/test/scala/zio/metrics/PrometheusTest.scala +++ b/prometheus/src/test/scala/zio/metrics/PrometheusTest.scala @@ -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 { @@ -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)) } } ), diff --git a/statsd/src/test/scala/zio/metrics/ClientTest.scala b/statsd/src/test/scala/zio/metrics/ClientTest.scala index b3761ada..159f8e56 100644 --- a/statsd/src/test/scala/zio/metrics/ClientTest.scala +++ b/statsd/src/test/scala/zio/metrics/ClientTest.scala @@ -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] => diff --git a/statsd/src/test/scala/zio/metrics/DogStatsDClientTest.scala b/statsd/src/test/scala/zio/metrics/DogStatsDClientTest.scala index 5cc42d61..bd63fb79 100644 --- a/statsd/src/test/scala/zio/metrics/DogStatsDClientTest.scala +++ b/statsd/src/test/scala/zio/metrics/DogStatsDClientTest.scala @@ -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:")) diff --git a/statsd/src/test/scala/zio/metrics/DogStatsDEncoderTest.scala b/statsd/src/test/scala/zio/metrics/DogStatsDEncoderTest.scala index fcff9551..0c09e081 100644 --- a/statsd/src/test/scala/zio/metrics/DogStatsDEncoderTest.scala +++ b/statsd/src/test/scala/zio/metrics/DogStatsDEncoderTest.scala @@ -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)))) @@ -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"))) ) ) }, @@ -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"))) } }, @@ -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"))) } } diff --git a/statsd/src/test/scala/zio/metrics/StatsDClientTest.scala b/statsd/src/test/scala/zio/metrics/StatsDClientTest.scala index e1dad3c9..89195ae6 100644 --- a/statsd/src/test/scala/zio/metrics/StatsDClientTest.scala +++ b/statsd/src/test/scala/zio/metrics/StatsDClientTest.scala @@ -1,10 +1,14 @@ package zio.metrics import zio.clock.Clock +import zio.console.putStrLn import zio.metrics.encoders._ import zio.metrics.statsd._ import zio.test._ import zio.test.Assertion._ +import zio.duration._ +import zio.test.TestAspect.{ flaky, forked, timeout } +import zio.URIO object StatsDClientTest extends DefaultRunnableSpec { private val port = 8922 @@ -12,24 +16,30 @@ object StatsDClientTest extends DefaultRunnableSpec { override def spec = suite("StatsDClient")( testM("sends correct metrics via UDP") { - val clientWithAgent = for { - d <- StatsDClient(500, 5000, 100, Some("localhost"), Some(port)) - u <- UDPAgent(port) - } yield (d, u) + val clientWithAgent = ( + for { + u <- UDPAgent(port) + d <- StatsDClient(500, 5000, 100, Some("localhost"), Some(port)) + _ <- putStrLn("Make is ready").toManaged(_ => URIO.unit) + } yield (d, u) + ) clientWithAgent.use { case (client, agent) => for { - _ <- client.increment("TestCounter", 0.9) + _ <- putStrLn("Starting use") + _ <- client.increment("TestCounter", 0.9, false) clientMetric <- agent.nextReceivedMetric + _ <- putStrLn("Testing timer") _ <- client.timer("TestTimer", 0.44, 0.9) timerMetric <- agent.nextReceivedMetric + _ <- putStrLn("Finished use") } yield { - assert(clientMetric)(equalTo("TestCounter:1|c|@0,9")) && - assert(timerMetric)(equalTo("TestTimer:0,44|ms|@0,9")) + assert(clientMetric)(equalTo("TestCounter:1|c|@0.9")) && + assert(timerMetric)(equalTo("TestTimer:0.44|ms|@0.9")) } } - } - ).provideCustomLayer(Encoder.statsd ++ Clock.live) + }.provideCustomLayer(Encoder.statsd ++ Clock.live) @@ forked @@ timeout(5.seconds) @@ flaky(5) + ) } diff --git a/statsd/src/test/scala/zio/metrics/StatsDEncoderTest.scala b/statsd/src/test/scala/zio/metrics/StatsDEncoderTest.scala index ce12e9d8..e3528e1e 100644 --- a/statsd/src/test/scala/zio/metrics/StatsDEncoderTest.scala +++ b/statsd/src/test/scala/zio/metrics/StatsDEncoderTest.scala @@ -13,7 +13,7 @@ object StatsDEncoderTest extends DefaultRunnableSpec { testCounter.map( tup => assert(tup._1)(isSome(equalTo("foobar:1|c"))) && - assert(tup._2)(isSome(equalTo("foobar:1|c|@0,5"))) + assert(tup._2)(isSome(equalTo("foobar:1|c|@0.5"))) ) }, testM("StatsD Encoder encodes gauges") {