distributiontable_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package cluster
  11. import (
  12. "fmt"
  13. "testing"
  14. )
  15. func TestDistributionTable(t *testing.T) {
  16. // Test simple case - 3 members with replication factor of 2 (normal uint64 location range)
  17. dt, _ := NewDistributionTable([]string{"a", "b", "c"}, 2)
  18. if dts := dt.String(); dts != `
  19. Location ranges:
  20. a: 0 -> 6148914691236517204
  21. b: 6148914691236517205 -> 12297829382473034409
  22. c: 12297829382473034410 -> 18446744073709551615
  23. Replicas (factor=2) :
  24. a: [b]
  25. b: [c]
  26. c: [a]
  27. `[1:] {
  28. t.Error("Unexpected distribution table:", dts)
  29. return
  30. }
  31. // 6 members with replication factor of 4 (location range of 30)
  32. dt, _ = createDistributionTable([]string{"a", "b", "c", "d", "e", "f"}, 4, 30)
  33. if dts := dt.String(); dts != `
  34. Location ranges:
  35. a: 0 -> 4
  36. b: 5 -> 9
  37. c: 10 -> 14
  38. d: 15 -> 19
  39. e: 20 -> 24
  40. f: 25 -> 30
  41. Replicas (factor=4) :
  42. a: [b c d]
  43. b: [c d e]
  44. c: [d e f]
  45. d: [e f a]
  46. e: [f a b]
  47. f: [a b c]
  48. `[1:] {
  49. t.Error("Unexpected distribution table:", dts)
  50. return
  51. }
  52. // Check other functions
  53. if mr := fmt.Sprint(dt.MemberRange("f")); mr != "25 30" {
  54. t.Error("Unexpected member range:", mr)
  55. return
  56. }
  57. if mr := fmt.Sprint(dt.MemberRange("a")); mr != "0 4" {
  58. t.Error("Unexpected member range:", mr)
  59. return
  60. }
  61. if mr := fmt.Sprint(dt.MemberRange("c")); mr != "10 14" {
  62. t.Error("Unexpected member range:", mr)
  63. return
  64. }
  65. if r := fmt.Sprint(dt.Replicas("c")); r != "[d e f]" {
  66. t.Error("Unexpected replicas:", r)
  67. return
  68. }
  69. if lh := fmt.Sprint(dt.LocationHome(24)); lh != "e[f a b]" {
  70. t.Error("Unexpected location home:", lh)
  71. return
  72. }
  73. if lh := fmt.Sprint(dt.LocationHome(20)); lh != "e[f a b]" {
  74. t.Error("Unexpected location home:", lh)
  75. return
  76. }
  77. if lh := fmt.Sprint(dt.LocationHome(0)); lh != "a[b c d]" {
  78. t.Error("Unexpected location home:", lh)
  79. return
  80. }
  81. if lh := fmt.Sprint(dt.LocationHome(40)); lh != "f[a b c]" {
  82. t.Error("Unexpected location home:", lh)
  83. return
  84. }
  85. if om := fmt.Sprint(dt.OtherReplicationMembers(20, "f")); om != "[e a b]" {
  86. t.Error("Unexpected other replication members:", om)
  87. return
  88. }
  89. if om := fmt.Sprint(dt.OtherReplicationMembers(20, "e")); om != "[f a b]" {
  90. t.Error("Unexpected other replication members:", om)
  91. return
  92. }
  93. if mr := fmt.Sprint(dt.ReplicationRange("c")); mr != "0 30" {
  94. t.Error("Unexpected member range:", mr)
  95. return
  96. }
  97. // 2 members with replication factor of 2 (location range of 30)
  98. dt, _ = createDistributionTable([]string{"a", "b"}, 2, 30)
  99. if dts := dt.String(); dts != `
  100. Location ranges:
  101. a: 0 -> 14
  102. b: 15 -> 30
  103. Replicas (factor=2) :
  104. a: [b]
  105. b: [a]
  106. `[1:] {
  107. t.Error("Unexpected distribution table:", dts)
  108. return
  109. }
  110. // 2 members with replication factor of 1 (location range of 30)
  111. dt, _ = createDistributionTable([]string{"a", "b"}, 1, 30)
  112. if dts := dt.String(); dts != `
  113. Location ranges:
  114. a: 0 -> 14
  115. b: 15 -> 30
  116. Replicas (factor=1) :
  117. a: []
  118. b: []
  119. `[1:] {
  120. t.Error("Unexpected distribution table:", dts)
  121. return
  122. }
  123. // 1 members with replication factor of 1 (location range of 30)
  124. dt, _ = createDistributionTable([]string{"a"}, 1, 30)
  125. if dts := dt.String(); dts != `
  126. Location ranges:
  127. a: 0 -> 30
  128. Replicas (factor=1) :
  129. a: []
  130. `[1:] {
  131. t.Error("Unexpected distribution table:", dts)
  132. return
  133. }
  134. // Error cases
  135. _, err := createDistributionTable([]string{"a"}, 0, 30)
  136. if err.Error() != "Replication factor must be > 0" {
  137. t.Error("Unexpected result:", err)
  138. return
  139. }
  140. _, err = createDistributionTable([]string{"a", "b"}, 3, 30)
  141. if err.Error() != "Not enough members (2) for given replication factor: 3" {
  142. t.Error("Unexpected result:", err)
  143. return
  144. }
  145. }