-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathReplicatedShoppingCartSpec.scala
178 lines (140 loc) · 5.56 KB
/
ReplicatedShoppingCartSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
*/
package akka.contrib.datareplication
import scala.concurrent.duration._
import com.typesafe.config.ConfigFactory
import akka.actor.Actor
import akka.actor.ActorRef
import akka.actor.Props
import akka.actor.Stash
import akka.cluster.Cluster
import akka.remote.testconductor.RoleName
import akka.remote.testkit.MultiNodeConfig
import akka.remote.testkit.MultiNodeSpec
import akka.testkit._
object ReplicatedShoppingCartSpec extends MultiNodeConfig {
val node1 = role("node-1")
val node2 = role("node-2")
val node3 = role("node-3")
commonConfig(ConfigFactory.parseString("""
akka.loglevel = INFO
akka.actor.provider = "akka.cluster.ClusterActorRefProvider"
akka.log-dead-letters-during-shutdown = off
"""))
}
object ShoppingCart {
def props(userId: String): Props = Props(new ShoppingCart(userId))
case object GetCart
case class AddItem(item: LineItem)
case class RemoveItem(productId: String)
case class Cart(items: Set[LineItem])
case class LineItem(productId: String, title: String, quantity: Int)
}
class ShoppingCart(userId: String) extends Actor with Stash {
import ShoppingCart._
import akka.contrib.datareplication.Replicator._
val replicator = DataReplication(context.system).replicator
implicit val cluster = Cluster(context.system)
val timeout = 3.seconds
val DataKey = "cart-" + userId
def receive = {
case AddItem(item) ⇒
val update = Update(DataKey, LWWMap(), ReadQuorum, WriteQuorum, timeout, None) {
cart => updateCart(cart, item)
}
replicator ! update
case GetCart ⇒
replicator ! Get(DataKey, ReadQuorum, timeout, Some(sender()))
case GetSuccess(DataKey, data: LWWMap, Some(replyTo: ActorRef)) ⇒
val cart = Cart((data.entries.values map { case line: LineItem ⇒ line }).toSet)
replyTo ! cart
case NotFound(DataKey, Some(replyTo: ActorRef)) ⇒
replyTo ! Cart(Set.empty)
case GetFailure(DataKey, Some(replyTo: ActorRef)) ⇒
// ReadQuorum failure, try again with local read
replicator ! Get(DataKey, ReadOne, timeout, Some(replyTo))
case RemoveItem(productId) ⇒
val update = Update(DataKey, LWWMap(), ReadQuorum, WriteQuorum, timeout, None) {
_ - productId
}
replicator ! update
case _: UpdateSuccess | _: ReplicationUpdateFailure ⇒
// ReplicationUpdateFailure, will eventually be replicated
}
def updateCart(data: LWWMap, item: LineItem): LWWMap =
data.get(item.productId) match {
case Some(LineItem(_, _, existingQuantity)) ⇒
data + (item.productId -> item.copy(quantity = existingQuantity + item.quantity))
case None ⇒ data + (item.productId -> item)
case _ ⇒ throw new IllegalStateException
}
override def unhandled(msg: Any): Unit = msg match {
case e: UpdateFailure ⇒ throw new IllegalStateException("Unexpected failure: " + e)
case _ ⇒ super.unhandled(msg)
}
}
class ReplicatedShoppingCartSpecMultiJvmNode1 extends ReplicatedShoppingCartSpec
class ReplicatedShoppingCartSpecMultiJvmNode2 extends ReplicatedShoppingCartSpec
class ReplicatedShoppingCartSpecMultiJvmNode3 extends ReplicatedShoppingCartSpec
class ReplicatedShoppingCartSpec extends MultiNodeSpec(ReplicatedShoppingCartSpec) with STMultiNodeSpec with ImplicitSender {
import ReplicatedShoppingCartSpec._
import ShoppingCart._
override def initialParticipants = roles.size
val cluster = Cluster(system)
val shoppingCart = system.actorOf(ShoppingCart.props("user-1"))
def join(from: RoleName, to: RoleName): Unit = {
runOn(from) {
cluster join node(to).address
}
enterBarrier(from.name + "-joined")
}
"Demo of a replicated shopping cart" must {
"join cluster" in {
join(node1, node1)
join(node2, node1)
join(node3, node1)
enterBarrier("after-1")
}
"handle updates directly after start" in within(15.seconds) {
runOn(node2) {
shoppingCart ! ShoppingCart.AddItem(LineItem("1", "Apples", quantity = 2))
shoppingCart ! ShoppingCart.AddItem(LineItem("2", "Oranges", quantity = 3))
}
enterBarrier("updates-done")
awaitAssert {
shoppingCart ! ShoppingCart.GetCart
val cart = expectMsgType[Cart]
cart.items should be(Set(LineItem("1", "Apples", quantity = 2), LineItem("2", "Oranges", quantity = 3)))
}
enterBarrier("after-2")
}
"handle updates from different nodes" in within(5.seconds) {
runOn(node2) {
shoppingCart ! ShoppingCart.AddItem(LineItem("1", "Apples", quantity = 5))
shoppingCart ! ShoppingCart.RemoveItem("2")
}
runOn(node3) {
shoppingCart ! ShoppingCart.AddItem(LineItem("3", "Bananas", quantity = 4))
}
enterBarrier("updates-done")
awaitAssert {
shoppingCart ! ShoppingCart.GetCart
val cart = expectMsgType[Cart]
cart.items should be(Set(LineItem("1", "Apples", quantity = 7), LineItem("3", "Bananas", quantity = 4)))
}
enterBarrier("after-3")
}
"read own updates" in within(5.seconds) {
runOn(node2) {
shoppingCart ! ShoppingCart.AddItem(LineItem("1", "Apples", quantity = 1))
shoppingCart ! ShoppingCart.RemoveItem("3")
shoppingCart ! ShoppingCart.AddItem(LineItem("3", "Bananas", quantity = 5))
shoppingCart ! ShoppingCart.GetCart
val cart = expectMsgType[Cart]
cart.items should be(Set(LineItem("1", "Apples", quantity = 8), LineItem("3", "Bananas", quantity = 5)))
}
enterBarrier("after-4")
}
}
}