Skip to content

Commit

Permalink
Address cats/scalatest issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
matthughes committed Mar 4, 2020
1 parent 9baf3a3 commit 4448498
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ target
*~
.metals
project/.bloop
project/metals.sbt
.bloop
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ package com.ccadllc.cedi.circuitbreaker

import cats.effect.IO

import org.scalatest.WordSpec

import scala.concurrent.duration._
import org.scalatest.wordspec.AnyWordSpec

class CircuitBreakerRegistryTest extends WordSpec with TestSupport {
class CircuitBreakerRegistryTest extends AnyWordSpec with TestSupport {
"The circuit breaker registry" should {
"register a new failure circuit breaker when an existing one with the given identifier does not exist" in {
val id = CircuitBreaker.Identifier("test")
val registry = CircuitBreakerRegistry.create[IO](testRegistryConfig).unsafeRunSync
registry.circuitBreakers.unsafeRunSync shouldBe 'empty
registry.circuitBreakers.unsafeRunSync shouldBe Symbol("empty")
val cb = registry.forFailure(id, testFailureConfig).unsafeRunSync
registry.circuitBreakers.unsafeRunSync.get(id) shouldBe Some(cb)
}
Expand All @@ -44,12 +43,12 @@ class CircuitBreakerRegistryTest extends WordSpec with TestSupport {
val cb = registry.forFailure(id, testFailureConfig).unsafeRunSync
registry.circuitBreakers.unsafeRunSync.get(id) shouldBe Some(cb)
registry.removeCircuitBreaker(id).unsafeRunSync
registry.circuitBreakers.unsafeRunSync.get(id) shouldBe 'empty
registry.circuitBreakers.unsafeRunSync.get(id) shouldBe Symbol("empty")
}
"register a new flow control circuit breaker when an existing one with the given identifier does not exist" in {
val id = CircuitBreaker.Identifier("test")
val registry = CircuitBreakerRegistry.create[IO](testRegistryConfig).unsafeRunSync
registry.circuitBreakers.unsafeRunSync shouldBe 'empty
registry.circuitBreakers.unsafeRunSync shouldBe Symbol("empty")
val cb = registry.forFlowControl(id, testFlowControlConfig).unsafeRunSync
registry.circuitBreakers.unsafeRunSync.get(id) shouldBe Some(cb)
}
Expand All @@ -67,14 +66,14 @@ class CircuitBreakerRegistryTest extends WordSpec with TestSupport {
val cb = registry.forFlowControl(id, testFlowControlConfig).unsafeRunSync
registry.circuitBreakers.unsafeRunSync.get(id) shouldBe Some(cb)
registry.removeCircuitBreaker(id).unsafeRunSync
registry.circuitBreakers.unsafeRunSync.get(id) shouldBe 'empty
registry.circuitBreakers.unsafeRunSync.get(id) shouldBe Symbol("empty")
}
"ignore removal of a non-existent circuit breaker" in {
val id = CircuitBreaker.Identifier("test")
val registry = CircuitBreakerRegistry.create[IO](testRegistryConfig).unsafeRunSync
registry.circuitBreakers.unsafeRunSync.get(id) shouldBe 'empty
registry.circuitBreakers.unsafeRunSync.get(id) shouldBe Symbol("empty")
registry.removeCircuitBreaker(id).unsafeRunSync
registry.circuitBreakers.unsafeRunSync.get(id) shouldBe 'empty
registry.circuitBreakers.unsafeRunSync.get(id) shouldBe Symbol("empty")
}
"request a stream of events (when events are available)" in {
val id = CircuitBreaker.Identifier("test")
Expand Down Expand Up @@ -102,13 +101,13 @@ class CircuitBreakerRegistryTest extends WordSpec with TestSupport {
val gcSettings = RegistrySettings.GarbageCollection(gcCheckInterval, inactivityCutoff)
val registry = CircuitBreakerRegistry.create[IO](testRegistryConfig.copy(garbageCollection = gcSettings)).unsafeRunSync
registry.forFailure(id, testFailureConfig).unsafeRunSync
registry.circuitBreakers.unsafeRunSync should not be 'empty
registry.circuitBreakers.unsafeRunSync should not be Symbol("empty")
Thread.sleep(inactivityCutoff.toMillis + (gcCheckInterval.toMillis * 4L))
registry.circuitBreakers.unsafeRunSync shouldBe 'empty
registry.circuitBreakers.unsafeRunSync shouldBe Symbol("empty")
registry.forFailure(id, testFailureConfig).unsafeRunSync
registry.circuitBreakers.unsafeRunSync should not be 'empty
registry.circuitBreakers.unsafeRunSync should not be Symbol("empty")
Thread.sleep(inactivityCutoff.toMillis + (gcCheckInterval.toMillis * 4L))
registry.circuitBreakers.unsafeRunSync shouldBe 'empty
registry.circuitBreakers.unsafeRunSync shouldBe Symbol("empty")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ package com.ccadllc.cedi.circuitbreaker

import cats.effect.IO

import org.scalatest.WordSpec

import scala.concurrent.duration._
import org.scalatest.wordspec.AnyWordSpec

class FailureCircuitBreakerTest extends WordSpec with TestSupport {
class FailureCircuitBreakerTest extends AnyWordSpec with TestSupport {
"The failure circuit breaker" should {
"switch to the open position when the failure rate of the services it is protecting exceeds the configured threshold" in {
val id = CircuitBreaker.Identifier("test")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ package com.ccadllc.cedi.circuitbreaker
import cats.effect.IO

import org.scalatest.OptionValues._
import org.scalatest.WordSpec

import scala.concurrent.duration._
import org.scalatest.wordspec.AnyWordSpec

class FlowControlCircuitBreakerTest extends WordSpec with TestSupport {
class FlowControlCircuitBreakerTest extends AnyWordSpec with TestSupport {
"The flow control circuitbreaker" should {
"throttle down the request rate when the inbound rate continue to exceed the max acceptable rate" in {
val id = CircuitBreaker.Identifier("test")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ import scala.collection.immutable.Vector
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scala.util.Random
import org.scalatest.{ BeforeAndAfterAll, Matchers, Suite, WordSpecLike }
import org.scalatest.{ BeforeAndAfterAll, Suite }
import CircuitBreaker._
import statistics._
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike

trait TestSupport extends WordSpecLike with Matchers with BeforeAndAfterAll {
trait TestSupport extends AnyWordSpecLike with Matchers with BeforeAndAfterAll {
self: Suite =>

private val GroupTasksBy = 10
Expand Down Expand Up @@ -163,7 +165,7 @@ trait TestSupport extends WordSpecLike with Matchers with BeforeAndAfterAll {
Random.shuffle(successfulTasks ++ failedTasks).grouped(groupedTasks).toVector.traverse[IO, Vector[Option[Throwable]]] {
def protectInParallel(taskGroup: Vector[IO[Unit]]) = {
def attemptProtect(t: IO[Unit]): IO[Option[Throwable]] = cb.protect(t).attempt map { _.left.toOption }
taskGroup.map(attemptProtect).parTraverse[IO, IO.Par, Option[Throwable]](identity)
taskGroup.map(attemptProtect).parTraverse[IO, Option[Throwable]](identity)
}
protectInParallel(_) map { r =>
Thread.sleep(50L)
Expand Down Expand Up @@ -201,7 +203,7 @@ trait TestSupport extends WordSpecLike with Matchers with BeforeAndAfterAll {
ec.tasks.grouped(groupedTasks).toVector.traverse[IO, Vector[IO[Either[Throwable, Unit]]]] {
def protectInParallel(taskGroup: Vector[IO[Unit]]) = {
def attemptProtect(t: IO[Unit]): IO[IO[Either[Throwable, Unit]]] = Concurrent[IO].start(cb.protect(t).attempt).map(_.join)
taskGroup.map(attemptProtect).parTraverse[IO, IO.Par, IO[Either[Throwable, Unit]]](identity)
taskGroup.map(attemptProtect).parTraverse[IO, IO[Either[Throwable, Unit]]](identity)
}
protectInParallel(_) map { r =>
Thread.sleep(pauseBetweenTaskGroups.toMillis)
Expand Down

0 comments on commit 4448498

Please sign in to comment.