vectorclock_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package sortutil
  10. import "testing"
  11. type dinnerDay struct {
  12. day string
  13. vc *VectorClock
  14. }
  15. const (
  16. actorAlice = "Alice"
  17. actorBen = "Ben"
  18. actorCathy = "Cathy"
  19. actorDave = "Dave"
  20. )
  21. /*
  22. The dinner agreement example was taken from:
  23. http://basho.com/posts/technical/why-vector-clocks-are-easy/
  24. */
  25. func TestDinnerAgreement(t *testing.T) {
  26. // Test how Alice, Ben Cathy and Dave are meeting for dinner at Dave's place
  27. // Start by Alice suggesting to meet on Wednesday
  28. dd := &dinnerDay{"Wednesday", NewVectorClock()}
  29. dd.vc.Act(actorAlice)
  30. dd2 := &dinnerDay{dd.day, CloneVectorClock(dd.vc)}
  31. // Ben suggests now Tuesday
  32. dd.day = "Tuesday"
  33. dd.vc.Act(actorBen)
  34. // Dave confirms the day
  35. dd.vc.Act(actorDave)
  36. // Check descendancy
  37. if !dd.vc.IsDescendent(dd2.vc) {
  38. t.Error("dd should be a descendent of dd2")
  39. return
  40. } else if dd2.vc.IsDescendent(dd.vc) {
  41. t.Error("dd2 should not be a descendent of dd")
  42. return
  43. }
  44. // Cathy has an old version and suggests Thursday
  45. dd2.day = "Thursday"
  46. dd2.vc.Act(actorCathy)
  47. // Detect conflict
  48. if !dd.vc.IsConflicting(dd2.vc) {
  49. t.Error("Vector clocks should be conflicting")
  50. return
  51. }
  52. // Dave makes a decision and chooses Thursday
  53. dd3 := &dinnerDay{dd2.day, NewDescendant(dd.vc, dd2.vc)}
  54. dd3.vc.Act(actorDave)
  55. // Check descendancy
  56. if !dd3.vc.IsDescendent(dd.vc) || dd3.vc.IsConflicting(dd.vc) {
  57. t.Error("dd3 should be a descendent of dd")
  58. return
  59. } else if !dd3.vc.IsDescendent(dd2.vc) || dd3.vc.IsConflicting(dd2.vc) {
  60. t.Error("dd3 should be a descendent of dd2")
  61. return
  62. }
  63. if out := dd3.vc.String(); out != `
  64. Alice:1
  65. Ben:1
  66. Cathy:1
  67. Dave:2
  68. `[1:] {
  69. t.Error("Unexpected output:", out)
  70. return
  71. }
  72. }