token_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. "bytes"
  7. "math/big"
  8. "sort"
  9. "strconv"
  10. "testing"
  11. )
  12. // Tests of the murmur3Patitioner
  13. func TestMurmur3Partitioner(t *testing.T) {
  14. token := murmur3Partitioner{}.ParseString("-1053604476080545076")
  15. if "-1053604476080545076" != token.String() {
  16. t.Errorf("Expected '-1053604476080545076' but was '%s'", token)
  17. }
  18. // at least verify that the partitioner
  19. // doesn't return nil
  20. pk, _ := marshalInt(nil, 1)
  21. token = murmur3Partitioner{}.Hash(pk)
  22. if token == nil {
  23. t.Fatal("token was nil")
  24. }
  25. }
  26. // Tests of the murmur3Token
  27. func TestMurmur3Token(t *testing.T) {
  28. if murmur3Token(42).Less(murmur3Token(42)) {
  29. t.Errorf("Expected Less to return false, but was true")
  30. }
  31. if !murmur3Token(-42).Less(murmur3Token(42)) {
  32. t.Errorf("Expected Less to return true, but was false")
  33. }
  34. if murmur3Token(42).Less(murmur3Token(-42)) {
  35. t.Errorf("Expected Less to return false, but was true")
  36. }
  37. }
  38. // Tests of the orderedPartitioner
  39. func TestOrderedPartitioner(t *testing.T) {
  40. // at least verify that the partitioner
  41. // doesn't return nil
  42. p := orderedPartitioner{}
  43. pk, _ := marshalInt(nil, 1)
  44. token := p.Hash(pk)
  45. if token == nil {
  46. t.Fatal("token was nil")
  47. }
  48. str := token.String()
  49. parsedToken := p.ParseString(str)
  50. if !bytes.Equal([]byte(token.(orderedToken)), []byte(parsedToken.(orderedToken))) {
  51. t.Errorf("Failed to convert to and from a string %s expected %x but was %x",
  52. str,
  53. []byte(token.(orderedToken)),
  54. []byte(parsedToken.(orderedToken)),
  55. )
  56. }
  57. }
  58. // Tests of the orderedToken
  59. func TestOrderedToken(t *testing.T) {
  60. if orderedToken([]byte{0, 0, 4, 2}).Less(orderedToken([]byte{0, 0, 4, 2})) {
  61. t.Errorf("Expected Less to return false, but was true")
  62. }
  63. if !orderedToken([]byte{0, 0, 3}).Less(orderedToken([]byte{0, 0, 4, 2})) {
  64. t.Errorf("Expected Less to return true, but was false")
  65. }
  66. if orderedToken([]byte{0, 0, 4, 2}).Less(orderedToken([]byte{0, 0, 3})) {
  67. t.Errorf("Expected Less to return false, but was true")
  68. }
  69. }
  70. // Tests of the randomPartitioner
  71. func TestRandomPartitioner(t *testing.T) {
  72. // at least verify that the partitioner
  73. // doesn't return nil
  74. p := randomPartitioner{}
  75. pk, _ := marshalInt(nil, 1)
  76. token := p.Hash(pk)
  77. if token == nil {
  78. t.Fatal("token was nil")
  79. }
  80. str := token.String()
  81. parsedToken := p.ParseString(str)
  82. if (*big.Int)(token.(*randomToken)).Cmp((*big.Int)(parsedToken.(*randomToken))) != 0 {
  83. t.Errorf("Failed to convert to and from a string %s expected %v but was %v",
  84. str,
  85. token,
  86. parsedToken,
  87. )
  88. }
  89. }
  90. // Tests of the randomToken
  91. func TestRandomToken(t *testing.T) {
  92. if ((*randomToken)(big.NewInt(42))).Less((*randomToken)(big.NewInt(42))) {
  93. t.Errorf("Expected Less to return false, but was true")
  94. }
  95. if !((*randomToken)(big.NewInt(41))).Less((*randomToken)(big.NewInt(42))) {
  96. t.Errorf("Expected Less to return true, but was false")
  97. }
  98. if ((*randomToken)(big.NewInt(42))).Less((*randomToken)(big.NewInt(41))) {
  99. t.Errorf("Expected Less to return false, but was true")
  100. }
  101. }
  102. type intToken int
  103. func (i intToken) String() string {
  104. return strconv.Itoa(int(i))
  105. }
  106. func (i intToken) Less(token token) bool {
  107. return i < token.(intToken)
  108. }
  109. // Test of the token ring implementation based on example at the start of this
  110. // page of documentation:
  111. // http://www.datastax.com/docs/0.8/cluster_architecture/partitioning
  112. func TestIntTokenRing(t *testing.T) {
  113. host0 := &HostInfo{}
  114. host25 := &HostInfo{}
  115. host50 := &HostInfo{}
  116. host75 := &HostInfo{}
  117. ring := &tokenRing{
  118. partitioner: nil,
  119. // these tokens and hosts are out of order to test sorting
  120. tokens: []token{
  121. intToken(0),
  122. intToken(50),
  123. intToken(75),
  124. intToken(25),
  125. },
  126. hosts: []*HostInfo{
  127. host0,
  128. host50,
  129. host75,
  130. host25,
  131. },
  132. }
  133. sort.Sort(ring)
  134. if ring.GetHostForToken(intToken(0)) != host0 {
  135. t.Error("Expected host 0 for token 0")
  136. }
  137. if ring.GetHostForToken(intToken(1)) != host25 {
  138. t.Error("Expected host 25 for token 1")
  139. }
  140. if ring.GetHostForToken(intToken(24)) != host25 {
  141. t.Error("Expected host 25 for token 24")
  142. }
  143. if ring.GetHostForToken(intToken(25)) != host25 {
  144. t.Error("Expected host 25 for token 25")
  145. }
  146. if ring.GetHostForToken(intToken(26)) != host50 {
  147. t.Error("Expected host 50 for token 26")
  148. }
  149. if ring.GetHostForToken(intToken(49)) != host50 {
  150. t.Error("Expected host 50 for token 49")
  151. }
  152. if ring.GetHostForToken(intToken(50)) != host50 {
  153. t.Error("Expected host 50 for token 50")
  154. }
  155. if ring.GetHostForToken(intToken(51)) != host75 {
  156. t.Error("Expected host 75 for token 51")
  157. }
  158. if ring.GetHostForToken(intToken(74)) != host75 {
  159. t.Error("Expected host 75 for token 74")
  160. }
  161. if ring.GetHostForToken(intToken(75)) != host75 {
  162. t.Error("Expected host 75 for token 75")
  163. }
  164. if ring.GetHostForToken(intToken(76)) != host0 {
  165. t.Error("Expected host 0 for token 76")
  166. }
  167. if ring.GetHostForToken(intToken(99)) != host0 {
  168. t.Error("Expected host 0 for token 99")
  169. }
  170. if ring.GetHostForToken(intToken(100)) != host0 {
  171. t.Error("Expected host 0 for token 100")
  172. }
  173. }
  174. // Test for the behavior of a nil pointer to tokenRing
  175. func TestNilTokenRing(t *testing.T) {
  176. var ring *tokenRing = nil
  177. if ring.GetHostForToken(nil) != nil {
  178. t.Error("Expected nil for nil token ring")
  179. }
  180. if ring.GetHostForPartitionKey(nil) != nil {
  181. t.Error("Expected nil for nil token ring")
  182. }
  183. }
  184. // Test of the recognition of the partitioner class
  185. func TestUnknownTokenRing(t *testing.T) {
  186. _, err := newTokenRing("UnknownPartitioner", nil)
  187. if err == nil {
  188. t.Error("Expected error for unknown partitioner value, but was nil")
  189. }
  190. }
  191. // Test of the tokenRing with the Murmur3Partitioner
  192. func TestMurmur3TokenRing(t *testing.T) {
  193. // Note, strings are parsed directly to int64, they are not murmur3 hashed
  194. var hosts []HostInfo = []HostInfo{
  195. HostInfo{
  196. Peer: "0",
  197. Tokens: []string{"0"},
  198. },
  199. HostInfo{
  200. Peer: "1",
  201. Tokens: []string{"25"},
  202. },
  203. HostInfo{
  204. Peer: "2",
  205. Tokens: []string{"50"},
  206. },
  207. HostInfo{
  208. Peer: "3",
  209. Tokens: []string{"75"},
  210. },
  211. }
  212. ring, err := newTokenRing("Murmur3Partitioner", hosts)
  213. if err != nil {
  214. t.Fatalf("Failed to create token ring due to error: %v", err)
  215. }
  216. p := murmur3Partitioner{}
  217. var actual *HostInfo
  218. actual = ring.GetHostForToken(p.ParseString("0"))
  219. if actual.Peer != "0" {
  220. t.Errorf("Expected peer 0 for token \"0\", but was %s", actual.Peer)
  221. }
  222. actual = ring.GetHostForToken(p.ParseString("25"))
  223. if actual.Peer != "1" {
  224. t.Errorf("Expected peer 1 for token \"25\", but was %s", actual.Peer)
  225. }
  226. actual = ring.GetHostForToken(p.ParseString("50"))
  227. if actual.Peer != "2" {
  228. t.Errorf("Expected peer 2 for token \"50\", but was %s", actual.Peer)
  229. }
  230. actual = ring.GetHostForToken(p.ParseString("75"))
  231. if actual.Peer != "3" {
  232. t.Errorf("Expected peer 3 for token \"01\", but was %s", actual.Peer)
  233. }
  234. actual = ring.GetHostForToken(p.ParseString("12"))
  235. if actual.Peer != "1" {
  236. t.Errorf("Expected peer 1 for token \"12\", but was %s", actual.Peer)
  237. }
  238. actual = ring.GetHostForToken(p.ParseString("24324545443332"))
  239. if actual.Peer != "0" {
  240. t.Errorf("Expected peer 0 for token \"24324545443332\", but was %s", actual.Peer)
  241. }
  242. }
  243. // Test of the tokenRing with the OrderedPartitioner
  244. func TestOrderedTokenRing(t *testing.T) {
  245. // Tokens here more or less are similar layout to the int tokens above due
  246. // to each numeric character translating to a consistently offset byte.
  247. var hosts []HostInfo = []HostInfo{
  248. HostInfo{
  249. Peer: "0",
  250. Tokens: []string{
  251. "00",
  252. },
  253. },
  254. HostInfo{
  255. Peer: "1",
  256. Tokens: []string{
  257. "25",
  258. },
  259. },
  260. HostInfo{
  261. Peer: "2",
  262. Tokens: []string{
  263. "50",
  264. },
  265. },
  266. HostInfo{
  267. Peer: "3",
  268. Tokens: []string{
  269. "75",
  270. },
  271. },
  272. }
  273. ring, err := newTokenRing("OrderedPartitioner", hosts)
  274. if err != nil {
  275. t.Fatalf("Failed to create token ring due to error: %v", err)
  276. }
  277. p := orderedPartitioner{}
  278. var actual *HostInfo
  279. actual = ring.GetHostForToken(p.ParseString("0"))
  280. if actual.Peer != "0" {
  281. t.Errorf("Expected peer 0 for token \"0\", but was %s", actual.Peer)
  282. }
  283. actual = ring.GetHostForToken(p.ParseString("25"))
  284. if actual.Peer != "1" {
  285. t.Errorf("Expected peer 1 for token \"25\", but was %s", actual.Peer)
  286. }
  287. actual = ring.GetHostForToken(p.ParseString("50"))
  288. if actual.Peer != "2" {
  289. t.Errorf("Expected peer 2 for token \"50\", but was %s", actual.Peer)
  290. }
  291. actual = ring.GetHostForToken(p.ParseString("75"))
  292. if actual.Peer != "3" {
  293. t.Errorf("Expected peer 3 for token \"01\", but was %s", actual.Peer)
  294. }
  295. actual = ring.GetHostForToken(p.ParseString("12"))
  296. if actual.Peer != "1" {
  297. t.Errorf("Expected peer 1 for token \"12\", but was %s", actual.Peer)
  298. }
  299. actual = ring.GetHostForToken(p.ParseString("24324545443332"))
  300. if actual.Peer != "1" {
  301. t.Errorf("Expected peer 1 for token \"24324545443332\", but was %s", actual.Peer)
  302. }
  303. }
  304. // Test of the tokenRing with the RandomPartitioner
  305. func TestRandomTokenRing(t *testing.T) {
  306. // String tokens are parsed into big.Int in base 10
  307. var hosts []HostInfo = []HostInfo{
  308. HostInfo{
  309. Peer: "0",
  310. Tokens: []string{
  311. "00",
  312. },
  313. },
  314. HostInfo{
  315. Peer: "1",
  316. Tokens: []string{
  317. "25",
  318. },
  319. },
  320. HostInfo{
  321. Peer: "2",
  322. Tokens: []string{
  323. "50",
  324. },
  325. },
  326. HostInfo{
  327. Peer: "3",
  328. Tokens: []string{
  329. "75",
  330. },
  331. },
  332. }
  333. ring, err := newTokenRing("RandomPartitioner", hosts)
  334. if err != nil {
  335. t.Fatalf("Failed to create token ring due to error: %v", err)
  336. }
  337. p := randomPartitioner{}
  338. var actual *HostInfo
  339. actual = ring.GetHostForToken(p.ParseString("0"))
  340. if actual.Peer != "0" {
  341. t.Errorf("Expected peer 0 for token \"0\", but was %s", actual.Peer)
  342. }
  343. actual = ring.GetHostForToken(p.ParseString("25"))
  344. if actual.Peer != "1" {
  345. t.Errorf("Expected peer 1 for token \"25\", but was %s", actual.Peer)
  346. }
  347. actual = ring.GetHostForToken(p.ParseString("50"))
  348. if actual.Peer != "2" {
  349. t.Errorf("Expected peer 2 for token \"50\", but was %s", actual.Peer)
  350. }
  351. actual = ring.GetHostForToken(p.ParseString("75"))
  352. if actual.Peer != "3" {
  353. t.Errorf("Expected peer 3 for token \"01\", but was %s", actual.Peer)
  354. }
  355. actual = ring.GetHostForToken(p.ParseString("12"))
  356. if actual.Peer != "1" {
  357. t.Errorf("Expected peer 1 for token \"12\", but was %s", actual.Peer)
  358. }
  359. actual = ring.GetHostForToken(p.ParseString("24324545443332"))
  360. if actual.Peer != "0" {
  361. t.Errorf("Expected peer 0 for token \"24324545443332\", but was %s", actual.Peer)
  362. }
  363. }