2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2020-01-02 07:24:59 -05:00
|
|
|
* Copyright (C) 2009-2020 Lightbend Inc. <https://www.lightbend.com>
|
2015-05-17 12:28:47 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package akka.cluster.ddata
|
|
|
|
|
|
2020-04-27 20:32:18 +08:00
|
|
|
import com.typesafe.config.ConfigFactory
|
|
|
|
|
import org.scalatest.BeforeAndAfterAll
|
|
|
|
|
import org.scalatest.matchers.should.Matchers
|
|
|
|
|
import org.scalatest.wordspec.AnyWordSpecLike
|
|
|
|
|
|
2015-05-17 12:28:47 +02:00
|
|
|
import akka.actor.Actor
|
|
|
|
|
import akka.actor.ActorSystem
|
|
|
|
|
import akka.actor.Props
|
|
|
|
|
import akka.actor.Stash
|
|
|
|
|
import akka.testkit.ImplicitSender
|
|
|
|
|
import akka.testkit.TestKit
|
|
|
|
|
|
|
|
|
|
object LocalConcurrencySpec {
|
|
|
|
|
|
|
|
|
|
final case class Add(s: String)
|
|
|
|
|
|
|
|
|
|
object Updater {
|
|
|
|
|
val key = ORSetKey[String]("key")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Updater extends Actor with Stash {
|
2018-12-14 15:53:08 -05:00
|
|
|
|
2020-04-05 11:55:25 +07:00
|
|
|
implicit val selfUniqueAddress: SelfUniqueAddress = DistributedData(context.system).selfUniqueAddress
|
2018-12-14 15:53:08 -05:00
|
|
|
|
2015-05-17 12:28:47 +02:00
|
|
|
val replicator = DistributedData(context.system).replicator
|
|
|
|
|
|
|
|
|
|
def receive = {
|
2019-02-09 15:25:39 +01:00
|
|
|
case s: String =>
|
2018-12-14 15:53:08 -05:00
|
|
|
val update = Replicator.Update(Updater.key, ORSet.empty[String], Replicator.WriteLocal)(_ :+ s)
|
2015-05-17 12:28:47 +02:00
|
|
|
replicator ! update
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
class LocalConcurrencySpec(_system: ActorSystem)
|
|
|
|
|
extends TestKit(_system)
|
2020-01-11 15:14:21 +03:00
|
|
|
with AnyWordSpecLike
|
2019-03-11 10:38:24 +01:00
|
|
|
with Matchers
|
|
|
|
|
with BeforeAndAfterAll
|
|
|
|
|
with ImplicitSender {
|
2015-05-17 12:28:47 +02:00
|
|
|
import LocalConcurrencySpec._
|
|
|
|
|
|
2020-04-03 00:13:12 +07:00
|
|
|
def this() =
|
2019-03-11 10:38:24 +01:00
|
|
|
this(
|
2019-03-13 10:56:20 +01:00
|
|
|
ActorSystem(
|
|
|
|
|
"LocalConcurrencySpec",
|
|
|
|
|
ConfigFactory.parseString("""
|
2016-06-10 15:04:13 +02:00
|
|
|
akka.actor.provider = "cluster"
|
2019-05-01 08:12:09 +01:00
|
|
|
akka.remote.classic.netty.tcp.port=0
|
2016-10-10 20:00:24 +02:00
|
|
|
akka.remote.artery.canonical.port = 0
|
2015-05-17 12:28:47 +02:00
|
|
|
""")))
|
|
|
|
|
|
|
|
|
|
override def afterAll(): Unit = {
|
|
|
|
|
shutdown(system)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val replicator = DistributedData(system).replicator
|
|
|
|
|
|
|
|
|
|
"Updates from same node" must {
|
|
|
|
|
|
|
|
|
|
"be possible to do from two actors" in {
|
2020-04-27 17:31:16 +07:00
|
|
|
val updater1 = system.actorOf(Props[Updater](), "updater1")
|
|
|
|
|
val updater2 = system.actorOf(Props[Updater](), "updater2")
|
2015-05-17 12:28:47 +02:00
|
|
|
|
|
|
|
|
val numMessages = 100
|
2019-02-09 15:25:39 +01:00
|
|
|
for (n <- 1 to numMessages) {
|
2015-05-17 12:28:47 +02:00
|
|
|
updater1 ! s"a$n"
|
|
|
|
|
updater2 ! s"b$n"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val expected = ((1 to numMessages).map("a" + _) ++ (1 to numMessages).map("b" + _)).toSet
|
|
|
|
|
awaitAssert {
|
|
|
|
|
replicator ! Replicator.Get(Updater.key, Replicator.ReadLocal)
|
|
|
|
|
val ORSet(elements) = expectMsgType[Replicator.GetSuccess[_]].get(Updater.key)
|
|
|
|
|
elements should be(expected)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|