token_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright (c) 2015 The gocql Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gocql
  5. import (
  6. "math/big"
  7. "strconv"
  8. "testing"
  9. )
  10. func TestMurmur3H1(t *testing.T) {
  11. assertMurmur3H1(t, []byte{}, 0x000000000000000)
  12. assertMurmur3H1(t, []byte{0}, 0x4610abe56eff5cb5)
  13. assertMurmur3H1(t, []byte{0, 1}, 0x7cb3f5c58dab264c)
  14. assertMurmur3H1(t, []byte{0, 1, 2}, 0xb872a12fef53e6be)
  15. assertMurmur3H1(t, []byte{0, 1, 2, 3}, 0xe1c594ae0ddfaf10)
  16. }
  17. func assertMurmur3H1(t *testing.T, data []byte, expected uint64) {
  18. actual := murmur3H1(data)
  19. if actual != expected {
  20. t.Errorf("Expected h1 = %x for data = %v, but was %x", expected, data, actual)
  21. }
  22. }
  23. func BenchmarkMurmur3H1(b *testing.B) {
  24. var h1 uint64
  25. var data [1024]byte
  26. for i := 0; i < 1024; i++ {
  27. data[i] = byte(i)
  28. }
  29. for i := 0; i < b.N; i++ {
  30. b.ResetTimer()
  31. h1 = murmur3H1(data[:])
  32. _ = Murmur3Token(int64(h1))
  33. }
  34. }
  35. func TestMurmur3Partitioner(t *testing.T) {
  36. token := Murmur3Partitioner{}.ParseString("-1053604476080545076")
  37. if "-1053604476080545076" != token.String() {
  38. t.Errorf("Expected '-1053604476080545076' but was '%s'", token)
  39. }
  40. // at least verify that the partitioner
  41. // doesn't return nil
  42. pk, _ := marshalInt(nil, 1)
  43. token = Murmur3Partitioner{}.Hash(pk)
  44. if token == nil {
  45. t.Fatal("token was nil")
  46. }
  47. }
  48. func TestMurmur3Token(t *testing.T) {
  49. if Murmur3Token(42).Less(Murmur3Token(42)) {
  50. t.Errorf("Expected Less to return false, but was true")
  51. }
  52. if !Murmur3Token(-42).Less(Murmur3Token(42)) {
  53. t.Errorf("Expected Less to return true, but was false")
  54. }
  55. if Murmur3Token(42).Less(Murmur3Token(-42)) {
  56. t.Errorf("Expected Less to return false, but was true")
  57. }
  58. }
  59. func TestOrderPreservingPartitioner(t *testing.T) {
  60. // at least verify that the partitioner
  61. // doesn't return nil
  62. pk, _ := marshalInt(nil, 1)
  63. token := OrderPreservingPartitioner{}.Hash(pk)
  64. if token == nil {
  65. t.Fatal("token was nil")
  66. }
  67. }
  68. func TestOrderPreservingToken(t *testing.T) {
  69. if OrderPreservingToken([]byte{0, 0, 4, 2}).Less(OrderPreservingToken([]byte{0, 0, 4, 2})) {
  70. t.Errorf("Expected Less to return false, but was true")
  71. }
  72. if !OrderPreservingToken([]byte{0, 0, 3}).Less(OrderPreservingToken([]byte{0, 0, 4, 2})) {
  73. t.Errorf("Expected Less to return true, but was false")
  74. }
  75. if OrderPreservingToken([]byte{0, 0, 4, 2}).Less(OrderPreservingToken([]byte{0, 0, 3})) {
  76. t.Errorf("Expected Less to return false, but was true")
  77. }
  78. }
  79. func TestRandomPartitioner(t *testing.T) {
  80. // at least verify that the partitioner
  81. // doesn't return nil
  82. pk, _ := marshalInt(nil, 1)
  83. token := RandomPartitioner{}.Hash(pk)
  84. if token == nil {
  85. t.Fatal("token was nil")
  86. }
  87. }
  88. func TestRandomToken(t *testing.T) {
  89. if ((*RandomToken)(big.NewInt(42))).Less((*RandomToken)(big.NewInt(42))) {
  90. t.Errorf("Expected Less to return false, but was true")
  91. }
  92. if !((*RandomToken)(big.NewInt(41))).Less((*RandomToken)(big.NewInt(42))) {
  93. t.Errorf("Expected Less to return true, but was false")
  94. }
  95. if ((*RandomToken)(big.NewInt(42))).Less((*RandomToken)(big.NewInt(41))) {
  96. t.Errorf("Expected Less to return false, but was true")
  97. }
  98. }
  99. type IntToken int
  100. func (i IntToken) String() string {
  101. return strconv.Itoa(int(i))
  102. }
  103. func (i IntToken) Less(token Token) bool {
  104. return i < token.(IntToken)
  105. }
  106. func TestIntTokenRing(t *testing.T) {
  107. // test based on example at the start of this page of documentation:
  108. // http://www.datastax.com/docs/0.8/cluster_architecture/partitioning
  109. host0 := &HostInfo{}
  110. host25 := &HostInfo{}
  111. host50 := &HostInfo{}
  112. host75 := &HostInfo{}
  113. tokenRing := &TokenRing{
  114. partitioner: nil,
  115. tokens: []Token{
  116. IntToken(0),
  117. IntToken(25),
  118. IntToken(50),
  119. IntToken(75),
  120. },
  121. hosts: []*HostInfo{
  122. host0,
  123. host25,
  124. host50,
  125. host75,
  126. },
  127. }
  128. if tokenRing.GetHostForToken(IntToken(0)) != host0 {
  129. t.Error("Expected host 0 for token 0")
  130. }
  131. if tokenRing.GetHostForToken(IntToken(1)) != host25 {
  132. t.Error("Expected host 25 for token 1")
  133. }
  134. if tokenRing.GetHostForToken(IntToken(24)) != host25 {
  135. t.Error("Expected host 25 for token 24")
  136. }
  137. if tokenRing.GetHostForToken(IntToken(25)) != host25 {
  138. t.Error("Expected host 25 for token 25")
  139. }
  140. if tokenRing.GetHostForToken(IntToken(26)) != host50 {
  141. t.Error("Expected host 50 for token 26")
  142. }
  143. if tokenRing.GetHostForToken(IntToken(49)) != host50 {
  144. t.Error("Expected host 50 for token 49")
  145. }
  146. if tokenRing.GetHostForToken(IntToken(50)) != host50 {
  147. t.Error("Expected host 50 for token 50")
  148. }
  149. if tokenRing.GetHostForToken(IntToken(51)) != host75 {
  150. t.Error("Expected host 75 for token 51")
  151. }
  152. if tokenRing.GetHostForToken(IntToken(74)) != host75 {
  153. t.Error("Expected host 75 for token 74")
  154. }
  155. if tokenRing.GetHostForToken(IntToken(75)) != host75 {
  156. t.Error("Expected host 75 for token 75")
  157. }
  158. if tokenRing.GetHostForToken(IntToken(76)) != host0 {
  159. t.Error("Expected host 0 for token 76")
  160. }
  161. if tokenRing.GetHostForToken(IntToken(99)) != host0 {
  162. t.Error("Expected host 0 for token 99")
  163. }
  164. if tokenRing.GetHostForToken(IntToken(100)) != host0 {
  165. t.Error("Expected host 0 for token 100")
  166. }
  167. }