Guest User

Untitled

a guest
Apr 10th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.03 KB | None | 0 0
  1. package com.twitter.service.flock.edges.check
  2.  
  3. import scala.collection.immutable.HashMap
  4. import org.scalacheck._
  5. import net.lag.configgy.{Config, Configgy}
  6. import org.specs.{ScalaCheck, Specification}
  7.  
  8. object EdgesSpec extends Specification with ScalaCheck with Waiter {
  9. Configgy.configure(System.getProperty("basedir") + "/config/test.conf")
  10. val username = System.getProperty("db.test.user")
  11. val password = System.getProperty("db.test.password")
  12.  
  13. var edges: Edges = null
  14.  
  15. "Edges" should {
  16. doBefore {
  17. val config = Config.fromFile(System.getProperty("basedir") + "/config/" + System.getProperty("stage") + ".conf")
  18. val username = config("db.username")
  19. val password = config("db.password")
  20. val queryEvaluatorFactory = new QueryEvaluatorFactory(username, password)
  21. val nameServerQueryEvaluator = queryEvaluatorFactory(config("db.name_server_host"), config("db.name_server_database"))
  22.  
  23. val (e, k) = Edges(config.configMap("edges"), nameServerQueryEvaluator, queryEvaluatorFactory)
  24. edges = e
  25. }
  26.  
  27. doAfter {
  28. edges.stop_writes()
  29. val queryEvaluatorFactory = new QueryEvaluatorFactory(username, password)
  30. val hostConnection = queryEvaluatorFactory("localhost", null)
  31. hostConnection.execute("DROP DATABASE IF EXISTS flock_shard_005")
  32. }
  33.  
  34. "be (eventually) consistent" in {
  35. new Commands {
  36. case class State(forward: Map[(Long, Int), Set[Long]], backward: Map[(Long, Int), Set[Long]]) {
  37. def add (sourceId: Long, listId: Int, destinationId: Long) = {
  38. val forwardNew = forward.update((sourceId, listId), forward((sourceId, listId)) + destinationId)
  39. val backwardNew = backward.update((destinationId, listId), backward((destinationId, listId)) + sourceId)
  40. new State(forwardNew, backwardNew)
  41. }
  42. def remove(sourceId: Long, listId: Int, destinationId: Long) = {
  43. val forwardNew = forward.update((sourceId, listId), forward((sourceId, listId)) - destinationId)
  44. val backwardNew = backward.update((destinationId, listId), backward((destinationId, listId)) - sourceId)
  45. new State(forwardNew, backwardNew)
  46. }
  47. def count_of_destinations_for(sourceId: Long, listId: Int) = forward(sourceId, listId).size
  48. def count_of_sources_for(destinationId: Long, listId: Int) = backward(destinationId, listId).size
  49. def contains(sourceId: Long, listId: Int, destinationId: Long) = forward(sourceId, listId).contains(destinationId)
  50. }
  51.  
  52. def initialState = {
  53. val queryEvaluatorFactory = new QueryEvaluatorFactory(username, password)
  54. val hostConnection = queryEvaluatorFactory("localhost", null)
  55. hostConnection.execute("DROP DATABASE IF EXISTS flock_shard_005")
  56.  
  57. val forwardShard1 = new gen.ShardInfo ("table_005", "flock_shard_005", "localhost", "localhost")
  58. val backwardShard1 = new gen.ShardInfo("table_006", "flock_shard_005", "localhost", "localhost")
  59. val forwardShard2 = new gen.ShardInfo ("table_007", "flock_shard_005", "localhost", "localhost")
  60. val backwardShard2 = new gen.ShardInfo("table_008", "flock_shard_005", "localhost", "localhost")
  61. val forwardShard3 = new gen.ShardInfo ("table_009", "flock_shard_005", "localhost", "localhost")
  62. val backwardShard3 = new gen.ShardInfo("table_010", "flock_shard_005", "localhost", "localhost")
  63. val forwardShard4 = new gen.ShardInfo ("table_011", "flock_shard_005", "localhost", "localhost")
  64. val backwardShard4 = new gen.ShardInfo("table_012", "flock_shard_005", "localhost", "localhost")
  65. edges.create_shard(0, gen.List.Follows, forwardShard1, backwardShard1)
  66. edges.create_shard(0, gen.List.Blocks, forwardShard2, backwardShard2)
  67. edges.create_shard(0, gen.List.FollowsSms, forwardShard3, backwardShard3)
  68. edges.create_shard(0, gen.List.RequestsToFollow, forwardShard4, backwardShard4)
  69.  
  70. val map = new HashMap[(Long, Int), Set[Long]] withDefaultValue Set()
  71. State(map, map)
  72. }
  73.  
  74. case class Add(sourceId: Long, listId: Int, destinationId: Long) extends Command {
  75. val time = System.currentTimeMillis
  76.  
  77. def run(s: State) = {
  78. println("Adding " + (sourceId, listId, destinationId))
  79. edges.add_at(sourceId, listId, destinationId, time)
  80. }
  81.  
  82. def nextState(s: State) = s.add(sourceId, listId, destinationId)
  83.  
  84. preCondition = s => true
  85. postCondition = { (s, r) =>
  86. waitUntil { edges.contains(sourceId, listId, destinationId) }
  87. }
  88. }
  89.  
  90. case class Remove(sourceId: Long, listId: Int, destinationId: Long) extends Command {
  91. val time = System.currentTimeMillis
  92.  
  93. def run(s: State) = {
  94. println("Removing " + (sourceId, listId, destinationId))
  95. edges.remove_at(sourceId, listId, destinationId, time)
  96. }
  97.  
  98. def nextState(s: State) = s.remove(sourceId, listId, destinationId)
  99.  
  100. preCondition = s => true
  101. postCondition = { (s, r) =>
  102. waitUntil { !edges.contains(sourceId, listId, destinationId) }
  103. }
  104. }
  105.  
  106. case class Count(sourceId: Long, listId: Int) extends Command {
  107. def run(s: State) = {
  108. println("Count " + (sourceId, listId))
  109. }
  110.  
  111. def nextState(s: State) = s
  112.  
  113. preCondition = s => true
  114. postCondition = { (s, r) =>
  115. edges.count_of_destinations_for(sourceId, listId) == s.count_of_destinations_for(sourceId, listId)
  116. edges.count_of_sources_for(sourceId, listId) == s.count_of_sources_for(sourceId, listId)
  117. }
  118. }
  119.  
  120. case class Contains(sourceId: Long, listId: Int, destinationId: Long) extends Command {
  121. def run(s: State) = {
  122. println("Contains " + (sourceId, listId, destinationId))
  123. }
  124.  
  125. def nextState(s: State) = s
  126.  
  127. preCondition = s => true
  128. postCondition = { (s, r) =>
  129. edges.contains(sourceId, listId, destinationId) == s.contains(sourceId, listId, destinationId)
  130. }
  131. }
  132.  
  133. case class Get(sourceId: Long, listId: Int, destinationId: Long) extends Command {
  134. def run(s: State) = {
  135. println("Get " + (sourceId, listId, destinationId))
  136. }
  137.  
  138. def nextState(s: State) = s
  139.  
  140. preCondition = s => true
  141. postCondition = { (s, r) =>
  142. if (s.contains(sourceId, listId, destinationId)) {
  143. val membership = edges.get(sourceId, listId, destinationId)
  144. membership.destination_id == destinationId && membership.created_at <= System.currentTimeMillis
  145. } else {
  146. try {
  147. edges.get(sourceId, listId, destinationId)
  148. false
  149. } catch {
  150. case _ => true
  151. }
  152. }
  153. }
  154. }
  155.  
  156. val key = for (
  157. sourceId <- Gen.choose(1L, 10000L);
  158. destinationId <- Gen.choose(1L, 10000L);
  159. listId <- Gen.elements(gen.List.Follows, gen.List.Blocks, gen.List.RequestsToFollow, gen.List.FollowsSms)
  160. ) yield (sourceId, destinationId, listId)
  161.  
  162. val add = for (
  163. (sourceId, destinationId, listId) <- key
  164. ) yield Add(sourceId, listId, destinationId)
  165.  
  166. val remove = for (
  167. (sourceId, destinationId, listId) <- key
  168. ) yield Remove(sourceId, listId, destinationId)
  169.  
  170. val count = for (
  171. sourceId <- Gen.choose(1L, 10000L);
  172. listId <- Gen.elements(gen.List.Follows, gen.List.Blocks, gen.List.RequestsToFollow, gen.List.FollowsSms)
  173. ) yield Count(sourceId, listId)
  174.  
  175. val contains = for (
  176. (sourceId, destinationId, listId) <- key
  177. ) yield Contains(sourceId, listId, destinationId)
  178.  
  179. val get = for (
  180. (sourceId, destinationId, listId) <- key
  181. ) yield Get(sourceId, listId, destinationId)
  182.  
  183. def genCommand(s: State): Gen[Command] = for (
  184. command <- Gen.oneOf(add, remove, count, contains, get)
  185. ) yield command
  186.  
  187. }.check
  188. }
  189. }
  190. }
Add Comment
Please, Sign In to add comment