-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(tests): bad
DistributedCacheServiceTest
- Loading branch information
1 parent
7df6080
commit 2e0849f
Showing
7 changed files
with
130 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 107 additions & 54 deletions
161
src/test/scala/core/services/cache/DistributedCacheServiceSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,107 @@ | ||
import akka.actor.ActorSystem | ||
import akka.cluster.ddata.{DistributedData, SelfUniqueAddress} | ||
import akka.testkit.TestKit | ||
import cats.effect.IO | ||
import cats.effect.unsafe.implicits.global | ||
import config.ConfigLoader.DefaultConfigLoader | ||
import core.domain.preprocessor.{Mechanism, MechanismId, Reaction, ReactionId} | ||
import core.services.cache.DistributedCacheService | ||
import org.scalatest.BeforeAndAfterAll | ||
import org.scalatest.funsuite.AnyFunSuite | ||
import org.scalatest.matchers.should.Matchers | ||
import scala.concurrent.duration.DurationInt | ||
|
||
class DistributedCacheServiceTest extends AnyFunSuite with Matchers with BeforeAndAfterAll { | ||
|
||
implicit val system: ActorSystem = ActorSystem("TestSystem", DefaultConfigLoader.pureConfig) | ||
implicit val selfUniqueAddress: SelfUniqueAddress = DistributedData(system).selfUniqueAddress | ||
|
||
val cacheService = new DistributedCacheService[IO] | ||
|
||
override def afterAll(): Unit = { | ||
TestKit.shutdownActorSystem(system) | ||
} | ||
|
||
test("putMechanism and getMechanism should store and retrieve a mechanism") { | ||
val mechanismId: MechanismId = 1 | ||
val mechanism: Mechanism = Mechanism(mechanismId, "mechanism", "type", 1.0) | ||
|
||
val result = for { | ||
_ <- cacheService.putMechanism(mechanismId, mechanism) | ||
_ <- IO.sleep(2.seconds) | ||
retrieved <- cacheService.getMechanism(mechanismId) | ||
} yield retrieved | ||
|
||
val retrievedMechanism = result.unsafeRunSync() | ||
|
||
retrievedMechanism shouldEqual Some(mechanism) | ||
} | ||
|
||
test("putReaction and getReaction should store and retrieve a reaction") { | ||
val reactionId: ReactionId = 1 | ||
val reaction: Reaction = Reaction(reactionId, "reaction") | ||
|
||
val result = for { | ||
_ <- cacheService.putReaction(reactionId, reaction) | ||
_ <- IO.sleep(2.seconds) | ||
retrieved <- cacheService.getReaction(reactionId) | ||
} yield retrieved | ||
|
||
val retrievedReaction = result.unsafeRunSync() | ||
|
||
retrievedReaction shouldEqual Some(reaction) | ||
} | ||
} | ||
// import akka.actor.ActorSystem | ||
// import akka.cluster.{Cluster, MemberStatus} | ||
// import akka.cluster.ddata.{DistributedData, SelfUniqueAddress} | ||
// import akka.testkit.TestKit | ||
|
||
// import cats.effect.IO | ||
// import cats.effect.unsafe.implicits.global | ||
|
||
// // import com.typesafe.config.ConfigFactory | ||
// import com.typesafe.config.Config | ||
|
||
// import config.ConfigLoader.DefaultConfigLoader | ||
|
||
// import core.domain.preprocessor.{Mechanism, MechanismId, Reaction, ReactionId} | ||
// import core.services.cache.DistributedCacheService | ||
|
||
// import org.scalatest.BeforeAndAfterAll | ||
// import org.scalatest.funsuite.AnyFunSuite | ||
// import org.scalatest.matchers.should.Matchers | ||
|
||
// import scala.concurrent.duration._ | ||
|
||
// class DistributedCacheServiceTest extends AnyFunSuite with Matchers with BeforeAndAfterAll { | ||
|
||
// // val config = ConfigFactory.parseString(s""" | ||
// // akka.actor.provider = "cluster" | ||
// // akka.remote.artery.canonical.port = 2001 | ||
// // akka.remote.artery.canonical.hostname = "localhost" | ||
// // akka.cluster.seed-nodes = [ | ||
// // "akka://TestActorSystem@127.0.0.1:2001" | ||
// // ] | ||
// // """).withFallback(ConfigFactory.load()) | ||
|
||
// val config: Config = DefaultConfigLoader.pureConfig | ||
// val system: ActorSystem = ActorSystem("ChemistFlowActorSystem", config) | ||
// val selfUniqueAddress: SelfUniqueAddress = DistributedData(system).selfUniqueAddress | ||
// val cacheService: DistributedCacheService[IO] = new DistributedCacheService[IO](system, selfUniqueAddress) | ||
// val cluster: Cluster = Cluster(system) | ||
|
||
// cluster.registerOnMemberUp { | ||
// println("Cluster is fully operational") | ||
// } | ||
|
||
// def ensureClusterIsReady(): Unit = { | ||
// val timeout = System.currentTimeMillis() + 5000 | ||
// while (System.currentTimeMillis() < timeout && !cluster.state.members.exists(_.status == MemberStatus.Up)) { | ||
// Thread.sleep(100) | ||
// } | ||
// if (!cluster.state.members.exists(_.status == MemberStatus.Up)) { | ||
// throw new RuntimeException("Cluster did not stabilise") | ||
// } | ||
// } | ||
|
||
// override def afterAll(): Unit = { | ||
// TestKit.shutdownActorSystem(system) | ||
// } | ||
|
||
// override protected def beforeAll(): Unit = { | ||
// ensureClusterIsReady() | ||
// } | ||
|
||
// test("putMechanism and getMechanism should store and retrieve a mechanism") { | ||
// val mechanismId: MechanismId = 1 | ||
// val mechanism: Mechanism = Mechanism(mechanismId, "mechanism", "type", 1.0) | ||
|
||
// val result = for { | ||
// _ <- cacheService.putMechanism(mechanismId, mechanism) | ||
// retrieved <- retryIO(10, 500.millis) { | ||
// cacheService.getMechanism(mechanismId).flatMap { | ||
// case Some(value) => IO.pure(value) | ||
// case None => IO.raiseError(new Exception("Mechanism not yet replicated")) | ||
// } | ||
// } | ||
// } yield retrieved | ||
|
||
// val retrievedMechanism = result.unsafeRunSync() | ||
|
||
// retrievedMechanism shouldEqual Some(mechanism) | ||
// } | ||
|
||
// test("putReaction and getReaction should store and retrieve a reaction") { | ||
// val reactionId: ReactionId = 1 | ||
// val reaction: Reaction = Reaction(reactionId, "reaction") | ||
|
||
// val result = for { | ||
// _ <- cacheService.putReaction(reactionId, reaction) | ||
// retrieved <- retryIO(10, 500.millis) { | ||
// cacheService.getReaction(reactionId).flatMap { | ||
// case Some(value) => IO.pure(value) | ||
// case None => IO.raiseError(new Exception("Reaction not yet replicated")) | ||
// } | ||
// } | ||
// } yield retrieved | ||
|
||
// val retrievedReaction = result.unsafeRunSync() | ||
|
||
// retrievedReaction shouldEqual Some(reaction) | ||
// } | ||
|
||
// private def retryIO[A](retries: Int, delay: FiniteDuration)(action: IO[A]): IO[A] = { | ||
// action.handleErrorWith { error => | ||
// if (retries > 0) IO.sleep(delay) *> retryIO(retries - 1, delay)(action) | ||
// else IO.raiseError(error) | ||
// } | ||
// } | ||
|
||
// } |