token_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. // assertMurmur3H1(t, []byte("hello"), 0xcbd8a7b341bd9b02)
  17. // assertMurmur3H1(t, []byte("hello, world"), 0x342fac623a5ebc8e)
  18. assertMurmur3H1(t, []byte("19 Jan 2038 at 3:14:07 AM"), 0xb89e5988b737affc)
  19. // assertMurmur3H1(t, []byte("The quick brown fox jumps over the lazy dog."), 0xcd99481f9ee902c9)
  20. }
  21. func assertMurmur3H1(t *testing.T, data []byte, expected uint64) {
  22. actual := murmur3H1(data)
  23. if actual != expected {
  24. t.Errorf("Expected h1 = %x for data = %x, but was %x", expected, data, actual)
  25. }
  26. }
  27. func BenchmarkMurmur3H1(b *testing.B) {
  28. var h1 uint64
  29. var data [1024]byte
  30. for i := 0; i < 1024; i++ {
  31. data[i] = byte(i)
  32. }
  33. for i := 0; i < b.N; i++ {
  34. b.ResetTimer()
  35. h1 = murmur3H1(data[:])
  36. _ = murmur3Token(int64(h1))
  37. }
  38. }
  39. func TestMurmur3Partitioner(t *testing.T) {
  40. token := murmur3Partitioner{}.ParseString("-1053604476080545076")
  41. if "-1053604476080545076" != token.String() {
  42. t.Errorf("Expected '-1053604476080545076' but was '%s'", token)
  43. }
  44. // at least verify that the partitioner
  45. // doesn't return nil
  46. pk, _ := marshalInt(nil, 1)
  47. token = murmur3Partitioner{}.Hash(pk)
  48. if token == nil {
  49. t.Fatal("token was nil")
  50. }
  51. }
  52. func TestMurmur3Token(t *testing.T) {
  53. if murmur3Token(42).Less(murmur3Token(42)) {
  54. t.Errorf("Expected Less to return false, but was true")
  55. }
  56. if !murmur3Token(-42).Less(murmur3Token(42)) {
  57. t.Errorf("Expected Less to return true, but was false")
  58. }
  59. if murmur3Token(42).Less(murmur3Token(-42)) {
  60. t.Errorf("Expected Less to return false, but was true")
  61. }
  62. }
  63. func TestOrderPreservingPartitioner(t *testing.T) {
  64. // at least verify that the partitioner
  65. // doesn't return nil
  66. pk, _ := marshalInt(nil, 1)
  67. token := orderPreservingPartitioner{}.Hash(pk)
  68. if token == nil {
  69. t.Fatal("token was nil")
  70. }
  71. }
  72. func TestOrderPreservingToken(t *testing.T) {
  73. if orderPreservingToken([]byte{0, 0, 4, 2}).Less(orderPreservingToken([]byte{0, 0, 4, 2})) {
  74. t.Errorf("Expected Less to return false, but was true")
  75. }
  76. if !orderPreservingToken([]byte{0, 0, 3}).Less(orderPreservingToken([]byte{0, 0, 4, 2})) {
  77. t.Errorf("Expected Less to return true, but was false")
  78. }
  79. if orderPreservingToken([]byte{0, 0, 4, 2}).Less(orderPreservingToken([]byte{0, 0, 3})) {
  80. t.Errorf("Expected Less to return false, but was true")
  81. }
  82. }
  83. func TestRandomPartitioner(t *testing.T) {
  84. // at least verify that the partitioner
  85. // doesn't return nil
  86. pk, _ := marshalInt(nil, 1)
  87. token := randomPartitioner{}.Hash(pk)
  88. if token == nil {
  89. t.Fatal("token was nil")
  90. }
  91. }
  92. func TestRandomToken(t *testing.T) {
  93. if ((*randomToken)(big.NewInt(42))).Less((*randomToken)(big.NewInt(42))) {
  94. t.Errorf("Expected Less to return false, but was true")
  95. }
  96. if !((*randomToken)(big.NewInt(41))).Less((*randomToken)(big.NewInt(42))) {
  97. t.Errorf("Expected Less to return true, but was false")
  98. }
  99. if ((*randomToken)(big.NewInt(42))).Less((*randomToken)(big.NewInt(41))) {
  100. t.Errorf("Expected Less to return false, but was true")
  101. }
  102. }
  103. type intToken int
  104. func (i intToken) String() string {
  105. return strconv.Itoa(int(i))
  106. }
  107. func (i intToken) Less(token token) bool {
  108. return i < token.(intToken)
  109. }
  110. func TestIntTokenRing(t *testing.T) {
  111. // test based on example at the start of this page of documentation:
  112. // http://www.datastax.com/docs/0.8/cluster_architecture/partitioning
  113. host0 := &HostInfo{}
  114. host25 := &HostInfo{}
  115. host50 := &HostInfo{}
  116. host75 := &HostInfo{}
  117. tokenRing := &tokenRing{
  118. partitioner: nil,
  119. tokens: []token{
  120. intToken(0),
  121. intToken(25),
  122. intToken(50),
  123. intToken(75),
  124. },
  125. hosts: []*HostInfo{
  126. host0,
  127. host25,
  128. host50,
  129. host75,
  130. },
  131. }
  132. if tokenRing.GetHostForToken(intToken(0)) != host0 {
  133. t.Error("Expected host 0 for token 0")
  134. }
  135. if tokenRing.GetHostForToken(intToken(1)) != host25 {
  136. t.Error("Expected host 25 for token 1")
  137. }
  138. if tokenRing.GetHostForToken(intToken(24)) != host25 {
  139. t.Error("Expected host 25 for token 24")
  140. }
  141. if tokenRing.GetHostForToken(intToken(25)) != host25 {
  142. t.Error("Expected host 25 for token 25")
  143. }
  144. if tokenRing.GetHostForToken(intToken(26)) != host50 {
  145. t.Error("Expected host 50 for token 26")
  146. }
  147. if tokenRing.GetHostForToken(intToken(49)) != host50 {
  148. t.Error("Expected host 50 for token 49")
  149. }
  150. if tokenRing.GetHostForToken(intToken(50)) != host50 {
  151. t.Error("Expected host 50 for token 50")
  152. }
  153. if tokenRing.GetHostForToken(intToken(51)) != host75 {
  154. t.Error("Expected host 75 for token 51")
  155. }
  156. if tokenRing.GetHostForToken(intToken(74)) != host75 {
  157. t.Error("Expected host 75 for token 74")
  158. }
  159. if tokenRing.GetHostForToken(intToken(75)) != host75 {
  160. t.Error("Expected host 75 for token 75")
  161. }
  162. if tokenRing.GetHostForToken(intToken(76)) != host0 {
  163. t.Error("Expected host 0 for token 76")
  164. }
  165. if tokenRing.GetHostForToken(intToken(99)) != host0 {
  166. t.Error("Expected host 0 for token 99")
  167. }
  168. if tokenRing.GetHostForToken(intToken(100)) != host0 {
  169. t.Error("Expected host 0 for token 100")
  170. }
  171. }